Adding Code to Plugin Files inside the core directory.

Adding code to files in the core directory/folder.

Init.php – the initialiser class

Open the Init.php file and add the following code.

Explaining the code.

We start our code by defining our package – Domain-Core 

We add the namespace Domain_Core.

Note

You might have noticed that we used first set the namespace to Core_Functions in the video. We later corrected this to Domain_Core.

The Init Class

We create a final class – Init. This class has to be a final because we will never extend this class. It has to be the only class in the core directory. As we will see, this class handles the initialization of all our classes/services.

The Init class has three methods.

  • get_services()
  • register_services()
  • Instantiate()

These functions/methods do the following.

get_services method

This class gets the services/classes we will be adding to our plugin. So the ReusableBlocks class inside the Admin directory will be added here, as we will see later in the course.

register_services method

This class runs a for loop for the services/classes obtained from the get_services method.

First, it instantiates the class and stores it in the service variable (by calling instantiate method)

It, thus, checks for the register method in the instantiated class stored in the service variable.

If it exists, it calls its register method.

At this stage, we have not yet seen the register method. We use this method to register any hooks and filters in our plugin. We will take a look at this later.

instantiate method

This method/function is called inside the register_services function. It simply instantiates a class and returns the instantiated class stored in the service variable. In the service variable, we check for the register method using the register_services method.