Architecture Foundations
Don’t forget that
we are working using a WordPress plugin…
Request Lifecycle
The entry point is the <plugin name>/{plugin-name}.php
. The {plugin-name}.php
contains the WordPress comments used to identify the plugin:
<?php
/**
* Plugin Name: WP Kirk
* Plugin URI: https://github.com/wpbones/WPKirk
* Description: WP Bones template WordPress plugin
* Version: 1.6.0
* Requires at least: 6.2
* Requires PHP: 7.4
* Author: Giovambattista Fazioli
* Author URI: https://wpbones.com/
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-kirk
* Domain Path: languages
*
*/
As you know, you may use any filename for your main PHP file. In fact, WordPress will recognize the main plugin file by the PHP comment in the file head.
The main plugin file will be renamed to your-plugin.php
by the WP Bones command line tool. For example, if you create a plugin named My Plugin
, the main file will be my-plugin.php
.
See Get plugin information for more details.
So, the entry point of a plugin is {your-plugin-name}.php
.
Of course, you can edit the {your-plugin-name}.php
file to set your WordPress comment information. The rest of the code is handled by WP Bones.
Plugin Structure
The default WP Bones plugin structure is intended to provide a great starting point for both large and small plugins. Of course, you are free to organize your application as you like. Similarly, Laravel, WP Bones imposes almost no restrictions on where any given class is located - as long as Composer can autoload the class.
The root directory
Let’s look at a default standard WP Bones plugin folder tree:
Additional directories
You may use the following additional directories:
Check out REST API and custom pages for more information.
The root files
Also, you will find a set of files in the root of the plugin:
- bones
command line PHP script
- composer.json
used by composer
- composer.lock
used by composer
- gulpfile.js
script used by Gulp
- {your-plugin-name}.php
entry point of plugin
- namespace
used by bones command
- package.json
used by npm
- readme.txt
WordPress readme file
- The
bones
file is the command line PHP script, likewiseartisan
in Laravel environment. - The
composer.json
andcomposer.lock
are used by Composer. - The
gulpfile.js
is the script used by Gulp. This file is prepared with the main tasks to process styles and scripts. Before using it, you should install Gulp and npm. - The
{your-plugin-name}.php
is the entry point of the plugin. - The
namespace
is a file used by the bones command. You can ignore it. - The
package.json
is used by npm. Feel free to add your own packages. - The
readme.txt
is the WordPress readme file to be used when you’ll submit your plugin to the WordPress repository.
The plugin directory
However, the most important folder is /plugin/
. In this folder, you will find the main controllers of your plugin:
- activation.php
- deactivation.php
The config directory
All of the configuration files for the plugin are stored in the config/
directory. Each option is self-documented, so feel free to look through the files and get familiar with the options available to you.
- menus.php
- plugin.php
…
In the WP Kirk template plugin, you will find a sample custom.php
file. This file contains:
<?php
if (!defined('ABSPATH')) {
exit();
}
/*
|--------------------------------------------------------------------------
| Custom configuration
|--------------------------------------------------------------------------
|
| This is an example of a custom configuration. You may get this configuration
| by plugin instance.
| For example, in a view you can use `$this->config( 'custom.sample' )`.
|
*/
return [
'sample' => 'Hello, Captain!'
];
It’s very easy to get a custom configuration by using $pluginInstance->config( 'custom.sample')
.
Plugin instance
The plugin instance is the most important object in WP Bones.
You will be able to get the plugin instance by using either WPKirk()
function or the global static class WPKirk
.
// plugin instance by global unique function
echo WPKirk()->Author;
// plugin instance by global static class
echo WPKirk::$plugin->Author;
Of course, if your plugin is named MyPlugin
you will use MyPlugin()
to get the instance. Same for the static class echo WPKirk::$plugin->Author;
Get plugin information
In the {your-plugin-name}.php
file you may use the comment to insert some plugin information:
<?php
/**
* Plugin Name: WP Kirk
* Plugin URI: https://github.com/wpbones/WPKirk
* Description: WP Bones template WordPress plugin
* Version: 1.6.0
* Requires at least: 6.2
* Requires PHP: 7.4
* Author: Giovambattista Fazioli
* Author URI: https://wpbones.com/
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-kirk
* Domain Path: languages
*
*/
You can get this information by using the plugin instance:
echo $plugin->Author;
echo $plugin->TextDomain;