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
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.
We use the composer.json file for mainly two things. These are;
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.
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 “-”
The composer.json specifies other metadata related to our project. These are;
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.
The description field describes the package.
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 property describes the license of this package. For example, this package is under a GPLv2 license.
Provides details such as the name, email, role and homepage.
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.
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;
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.
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.