Registering plugins and themes
You can work with Sense Framework in your WordPress plugins and themes.
Sense will treat both as "Sense Plugins" even in the case of WordPress Themes.
There is one special requirement: almost one active WordPress Plugin must to be registered in Sense in order to register your Theme in Sense.
Registering a Plugin
The first step to work with Sense in you Plugin is registering it, you can archive this in two simple steps:
1) First, you need to implement a class extending the Sense AbstractPlugin class:
// wp-content/plugins/your-plugin/your-plugin.php
class YourSensePlugin extends \Simettric\Sense\AbstractPlugin{
function getConfigLocations() {
return array( __DIR__ . '/Config' );
}
function getControllerLocations() {
return array( __DIR__ . '/Controller' );
}
function getName() {
return 'your-plugin-name';
}
}
By default, Sense will assume that you have this directory structure in your theme or plugin:
/your_plugin_or_theme_folder
----/Config
-------/services.yml
----/Controller
----/View
There are some special methods you can overwrite in order to configure and customize your plugin or theme directory structure:
Method name | Default value | Description |
---|---|---|
isTheme() | false | Determine if your plugin is a WordPress Plugin or a WordPress Theme. Sense will threat both WordPress concepts as a Sense Plugin. Important notice: WorPress Themes have some limitations and Sense needs almost one active WordPress Plugin registered as a Sense Plugin. |
getConfigLocations() | /Config | An array of directories where the services.yml file are located. |
getControllerLocations() | /Controller | An array of directories where the controllers are located. |
getTemplateLocations() | /View | An array of directories where the template files are located. |
registerPluggableFunctions() | void | You can implement here your custom WordPress pluggable functions. Not available to WordPress themes. |
getAdminTemplateLocations() | /AdminView | An array of directories where the template files for your wp-admin controllers are located. |
2) Then, you need to register this class in your pluginĀ“s main file:
// wp-content/plugins/your-plugin/your-plugin.php
class YourSensePlugin extends \Simettric\Sense\AbstractPlugin {
[...]
}
$sense = \Simettric\Sense\Kernel::getInstance();
$sense->getContainer()
->get("plugin_manager")
->register(new YourSensePlugin(), __FILE__ );
Note the second __FILE__ argument. This argument must to be the path of your WordPress Plugin main file where the headers are configured or the WordPress Theme functions file.
Registering a Theme
When you are using Sense in a WordPress Theme instead in a WordPress Plugin, you need to register your Plugin class in your functions.php file:
// wp-content/themes/your-theme/functions.php
class YourSensePlugin extends \Simettric\Sense\AbstractPlugin {
function isTheme() {
return true;
}
[...]
}
$sense = \Simettric\Sense\Kernel::getInstance();
$sense->getContainer()
->get("plugin_manager")
->register(new YourSensePlugin(), __FILE__ );
Using Composer class autoloader for your plugin or theme classes
We are going to add PHP files with several classes to our Controller
and View
directories. But for Sense to be able to recognise them, we have to use a PHP class autoloader. One way of dealing with this, is using the one already in Composer. For this, we have to edit the composer.json
file on your project's root directory, and add a section like this:
"autoload": {
"psr-4": { "Simettric\\Plugin\\" : "public/wp-content/plugins/mynewplugin/" }
}
You should change the route for the plugin or theme that you're developing. This way, all folder created inside it are recognised as sub-namespaces of `Simettric\Plugin,
like Simettric\Plugin\Controllerand
Simettric\Plugin\View``
After editing composer.json, you must run once composer install
on your project's root directory for it to register the path to be used by the autoloader.