How to use namespaces and composer autoload in WordPress Plugins

Posted on by Chandan Kumar

In a simple layman’s term namespaces are the labels for classes and functions. For example we define a variable inside a scope then this variable will be defined for that scope only, we can’t use it outside the scope. Just like a variable in a scope, we can use namespaces for class and functions. Namespaces avoids any collisions of class and functions.

What is composer?

Composer is a package manager. It gives you dynamic access to all the library you want to use in order to build better php scripts. In our case we will use the auto-load feature of composer in our plugin. It will give us ability to use namespaces . Then we can remove all the things like “require_once”, “include_once” etc. We will just use the name of a class to reference a file.

In this article we will see how we can develop in WordPress plugin using namespace.

  1. You have to install composer package manager if you don’t have it.
  2. Install and Type ‘composer’ in console to check its working.

After checking its working correctly. 

How to create custom plugin in WordPress step by step with Example

Follow these steps to implement autoload in our plugin.

-> open terminal/command-prompt and lets point to the directory/folder where our plugin is present. In our case my plugin name is AtiBlogTest. Then pass a command composer init. Then we can generate our composer.json file. You can call this file as a DNA of your php project. 

-> You can see some autocomplete in the square bracket. Because we have defined all the comments, package name etc. So composer is very smart and gives some autocomplete. You can press enter to use them or you can specify a different package name. 

  • I like the package name so I will press enter and move forward.
  • Description: My awesome project
  • Author: chandan<>
  • Minimum Stability: dev
  • Package type: project
  • Licence: GPL
  • Would you like to define your dependencies? : NO (If you have a really complex project which require a lot of dependencies then you can specify your dependencies here. But in our case we don’t really need any dependencies. we will just use the built-in composer autoload. )
  • Would you like to define your dev dependencies?: NO
  • Do you confirm generation? : yes

And there we go. We can see the composer.json file is generated.

-> Now , we will move ahead and open the file “composer.json” , that is newly generated by composer. We will specify the auto loading option , so that we can use namespaces and use the files without requiring them.

In this composer.json file, we will add the autoload option composer in the root object.

"auoload": {
"psr-4": {"Inc\\":"./inc"}

psr-4 is php scripting convention that allows us to use specific php functionality . Inside the psr-4 we have used the unique identifier we want to use in our plugin when we reference our plugins folder.

Here I have used “Inc” , but you can specify anything instead of “Inc”. Then We will specify this unique name “Inc” is referring to which folder. In our case it is the “inc” folder in our plugin. Now save this file .

-> Now, after structuring composer’s json file, we will install it. For that, we have to open the terminal/command prompt and change the directory to our plugin and just type “composer install”. Then we can see that it has generated a vendor folder. You can see inside the vendor folder it has autoload files. But do not touch these files. If you need to change the unique name in autoload , then change in the composer.json file and pass a command “composer dump-autoload”. It will regenerate the files.

-> Now we are ready to use the autoload functionality in our plugin files. 

In order to use the composer autoload, we will use the following code :

require_once dirname(__FILE__).'vendor/autoload.php';

Now we can define namespaces and reference that file through its own namespace without requiring the actual file. So, lets tackle the activation option first. 

Lets access the activate.php in inc folder and add namespace ” Inc ” . Also rename the class as simply Activate, without having any super-complicated name. This is the best practice that you should follow for the psr-4 convention. If you have a class and you’re using namespaces , then the class should have the same exact matching name of the file. So, we are using a class activate and file name is also activate.php. So, the psr-4 autoload will work . 

Now we will jump to main plugin file and use the namespace Activate just defined. Add the following code on top after the require autoload statement.

use Inc\Activate;

Also we will remove the require_once that is used for the activate. 

Do these for deactivate also. Now we are good to go. 

Similarly, you can do in the theme as well.

About ATI Base Author: Chandan Kumar

I'm a passionate Web Designer and Developer . I love to provide useful Knowledge and tutorials. Author of ATIBlog. Email : View all posts by Chandan Kumar →

Got Something To Say:

Your email address will not be published. Required fields are marked *



Planet Polaris

Website Support
website support at Planet Polaris