Adding Code to Plugin Files inside domain-core.

Adding code to files in the domain-core directory.

Now that we have set up the file structure. Let us add some code to the files we created in the previous topic.

We will start with the composer.json

composer.json

Open the composer.json file and paste the following code. This file is located at the root level of our plugin directory, as we saw in the previous topic.

Explaining the code

We use the composer.json file for mainly two things. These are;

  • Specify the dependencies of our plugin utilises using the require key. For now, our plugin is not using any third-party components/libraries, so this will be empty.
  • Autoloading classes using the autoload field.

Autoload property.

We use this field to map our namespace to the directory where all our classes are found. For example, and. In this tutorial, we will map our namespace Domain_Core to the core folder we created in the previous topic.

We will use the psr-4 autoloader.

To reference classes inside our plugin, such as ReusableBlock in ResuableBlock.php. We will use Domain_Core\\Admin\\ReusableBlock. This line of code will look up the code via this directory core/Admin/ReusableBlock.php.

To understand this, we have used the namespace Domain_Core to point to the core.

We will further see how the autoloading process works in the later stages of this lesson.

Note

Take note of the naming convention of the namespace. It should start with the capital letter and be separated by the underscore “_” and not a dash “-”

Other fields in composer.json

The composer.json specifies other metadata related to our project. These are;

Name field

The name field/property defines the name of the package. It is usually defined by the vendor/project-name. for this package the name is mrkwp/domain-core.

Here the vendor is mrkwp, and the project name is domain-core.

Description field

The description field describes the package.

Type field

The type fled describes the types of the package. In this case, it is a “project” type. By default, packages are assigned type – “library”. Other types supported by composer include meta package and composer plugin.

License field

License property describes the license of this package. For example, this package is under a GPLv2 license.

Authors field

Provides details such as the name, email, role and homepage.

Config field

We use this field to set another alternative directory for the vendor files. For example, in the code above, we set the vendor fields to be stored in the lib folder.

By default, vendor files are stored in the vendor directory. However, we have observed that our plugins used on sites hosted on WP Engine have issues when these files are stored inside the vendor directory. We, therefore, configure these to be stored in the lib folder.

Read more about the composer.json file in the Basic usage – composer documentation.

domain-core.php – main plugin file

Open the domain-core.php and add the following code.

We use the main plugin file to add details about our plugin in this file using the header comment lines 2 – 3. These details are;

  • Plugin Name
  • Plugin URI
  • Description
  • Author
  • Author URI
  • Text Domain
  • Domain Path
  • Version and Package

Read more about the header comment requirements in the WordPress documentation.

After the header comment block, we will add more lines of code to the domain-core.php to execute the following. 

  1. Line of code to prevent direct access to the file on the server – Line 15 and 16.
  2. Composer loader that will check for the existence of the autoloader and it requires it/ loads it. Lines – 18 – 21.
  3. The activation hook to run when the plugin is activated. Lines 23 – 31.
  4. The deactivation hook is to run when the plugin is deactivated. Lines 33- 45.
  5. Code to check for the Init class and then run the initialisation.

You should take note that on lines 29,39 and 47, we have referenced classes using the following syntax.

Domain_Core\Base\Activate – for the Activate Class

Domain_Core\Base\Deactivate – for the Deactivate Class

Domain_Core\Init – for the init Class

We have not used “core” anywhere because we already mapped it to our namespace Domain_Core in the composer.json.