Documentation
Core Concepts
Architecture Foundations

Architecture Foundations

đź’ˇ

Don't forget that we are working using a WordPress plugin.

Request Lifecycle

The entry point is the <plugin name>/index.php. The index.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.3.0
 * Author: Giovambattista Fazioli
 * Author URI: http://undolog.com
 * Text Domain: wp-kirk
 * Domain Path: localization
 *
 */

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. Anyway, I suggest you use index.php. See Get plugin information for more details.

So, the entry point of a plugin is index.php.

Of course, you can edit the index.php file in order to set your WordPress comment information. The rest of 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 where any given class is located - as long as Composer can autoload the class.

The root directory

Let's see 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'll find a set of files in the root of plugin:

    • bonescommand line PHP script
    • composer.jsonused by composer
    • composer.lockused by composer
    • gulpfile.jsscript used by Gulp
    • index.phpentry point of plugin
    • namespaceused by bones command
    • package.jsonused by npm
    • readme.txtWordPress readme file
    • The bones file is the command line PHP script, likewise artisan in Laravel environment.
    • The composer.json and composer.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 index.php is the entry point of plugin.
    • The namespace is a file used by 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'll find a sample custom.php file. This file contains:

    <?php
     
    /*
    |--------------------------------------------------------------------------
    | 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 index.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.3.0
     * Author: Giovambattista Fazioli
     * Author URI: http://undolog.com
     * Text Domain: wp-kirk
     * Domain Path: localization
     *
     */

    You can get this information by using the plugin instance:

    echo $plugin->Author;
    echo $plugin->TextDomain;