Quickly put, I’m trying to build an ‘eco-system’ using WordPress where I have a core plugin and then additional add-on plugins.
More in-depth, each add-on plugin will require the core plugin to function. I’ve achieved this using WordPress standard coding and file structure practices. I am reworking this project to now use Namespacing PSR-4, composer, bower and etc.
Standard WordPress Install
|
|__www
|
|___wp-admin
|
|___wp-content
| |
| |___plugins
| | |
| | |___my-core-plugin
| | | |
| | | |___library
| | | | |
| | | | |___class-post-register.php
| | | |
| | | |___vendor
| | | | |
| | | | |___autoload.php
| | | |
| | | |___composer.json
| | | |
| | | |___core.php
| | |
| | |___my-first-addon-plugin
| | | |
| | | |___library
| | | |
| | | |___vendor
| | | | |
| | | | |___autoload.php
| | | |
| | | |___composer.json
| | | |
| | | |___core.php
| | |
| | |___my-second-addon-plugin
| | | |
| | | |___library
| | | |
| | | |___vendor
| | | | |
| | | | |___autoload.php
| | | |
| | | |___composer.json
| | | |
| | | |___core.php
| | |
| |___themes
| | |
| | |___my-custom-theme
| |
| wp-includes
Core plugin psr4 via composer
"autoload": {
"psr-4": {
"CorePlugin\Library": "library"
}
}
Example core plugin class
<?php
namespace CorePluginlibrary;
class Post_Register {
private __construct() {
// ... code
}
private init() {
}
private register( $data ) {
// .. code to register a custom post for example.
}
}
First add-on plugin psr4 via composer
"autoload": {
"psr-4": {
"FirstAddon\Library": "library"
}
}
Class from an add-on plugin
Below is where I’m confused. I’m trying to use a class from the core plugin in a different namespace and I’m getting the error:
Fatal error: Class ‘CorePluginLibraryPost_Register’ not found in…
Both plugins autoload their respective composer generate autoload files so I though I’d be able to use
the namespaces. I came here to ask before I delve into this part of the PHP manual (http://php.net/manual/en/language.namespaces.php) where I’d try the sub namespacing perhaps.
<?php
namespace FirstAddon;
use CorePluginLibraryPost_Register;
class First_Addon {
private __construct() {
// ... code
}
private init() {
}
private another_function() {
}
}
Also, I’m hesitant to use the sub namespacing with brackets because, for example, in laravel, use
foobar; and use
barfoo; like so.
<?php namespace AppServices;
use AppUser;
use Validator;
use IlluminateContractsAuthRegistrar as RegistrarContract;
class Registrar implements RegistrarContract {
I’m sure you’ve moved on from this, but I thought I’d answer anyway, in case others try to have plugins depend on each other. I use classes and namespaces in my plugins. My plugins reuse each other’s classes.
Namespaced plugins
First, it basically it comes down to the order in which WordPress loads your plugins. I came from C#/Java myself, and was initially confused by how WP did things. You want to make sure the plugin you want to use, is already loaded. The best way to do this is to instantiate the class via a late hook – one that you know happens after plugins are loaded. An example could be
and then have the constructor use the class in the other plugin (or where ever you need it):
Dependencies
If plugins are dependent on each other, and needs to act differently depending on what plugins are enabled and what aren’t, you can do this:
First it makes sure that the function we need is loaded, and then checks if the relevant plugin is enabled.
AutoLoader
It’s worth mentioning that I use the built in autoloader:
.. which maps my class naming convention of My_Class to the filename my-class.php
It all works wonderfully.
Hope that helps someone.