Notes
- The OOP approach and namespace used in this development should avoid name conflicts with other plugins.
- The development is done under https://innovriver.com/test-astra.
- Plugin name is “IVR – _m_m_”.
- Plugin root folder is named “ivr__m_m_”.
- Plugin main php file MUST be named as “ivr__m_m_.php”. That is the file base name must be the same as the root folder name.
- The pattern “_m_m_” should be replaced with the actual name of the plugin:
- replace this pattern for all folder names, file names and file contents of the plugin.
- The actual name should be as short as possible.
- The actual name should be in lowercase and cannot have spaces and hyphen ( – ). Underscore ( _ ) is allowed.
- The ‘include’ folder is named as ‘inc_a101518’. Rename this folder as per the outline in Points to Note below. Find and replace in various files that refer to this folder.
Key Operations
- Load the relevant php files, including class files using the Composer autoload features.
- Load constants.php which defines the various Constants.
- define(‘OPTION_NAME’, ‘ivr-_m_m_’)
- ‘ivr-_m_m_’ is the field name for storing option settings in the wp_options db table.
- define(‘PLUGIN_FILE_NAME’, plugin_basename(dirname(__FILE__, 2)). ‘.php’)
- Extract the plugin main filename and define it as a constant.
- The extracted result should be the root directory name. Since the file base name is same as the directory name, append ‘.php’ to it serves the purpose.
- define(‘OPTION_NAME’, ‘ivr-_m_m_’)
- Register the Activation function. Currently this function does nothing.
- Register the Deactivation function. Currently this function does nothing.
- Add_shortcode — this registers the shortcode name and points to the shortcode function.
- Call the static function Init->register_services(). This will instantiate the classes return from the static function Init->get_services().
- The developer has to specify which classes need to be returned in the get_services( ).
- After the class is instantiated, the function register() of the class is called to run.
- Init->get_services( ) currently returns these classes:
- Base\Enqueue -> to enqueue the CSS and JS files
- Pages\Admin -> to generate Admin main and submain pages, and set up the field in wp_options db table for storing the admin settings. This field stores a 2D array to contain all possible settings.
- Base\SettingsLinks -> Setup the custom links for the plugin on the Plugin List page.
Points to Note
- The ‘include’ folder is named as ‘inc_a101518’. When the template is used for a real plugin, this should be changed to inc_aMMDDHH to indicate when the folder is created. This helps to prevent conflict with other plugins.
- Where ‘a’ stands for 2021, ‘b’ for 2022, and so on.
- MMDDHH stands for month, day and hour.
- The uninstall.php cannot access the global scope variables. Therefore this file has to require_once the ‘constants.php’ individually.
- The “return $this” in SettingsApi – > this returns the class object that allows the chaining of functions one after another in Admin->register( ) function.
Folders and Files
ivr__m_m_.php
- the main file of the plugin.
- Register the Activation and Deactivation functions
- Define the add_shortcode
- Instantiate concerned classes and run the register( ) functions of these classes.
uninstall.php
- During uninstall of the plugin, the records in the wp_options table created by the plugin are removed.
composer.json
- To map the primary namespace to the include folder.
- Autoload the classes in the primary and secondary namespaces using Composer.
assets/admin (Folder)
- Contain the CSS, JS and images for the Admin pages
assets/front (Folder)
- Contain the CSS, JS and images for the frontend pages
inc_a101518/constants.php
- Constants are defined here. The main file has to require_once this file.
- The uninstall.php cannot access the global scope variables. Therefore, uninstall.php has to require_once the ‘constants.php’ separately.
inc_a101518/Init.php
- The static register_services( ) function is called by the main plugin file. It get the classes returned from the get_services( ) function. For each of these returned classes, it instantiates the class and run the register function of the class.
- The static get_services( ) function returns these classes to the calling function:
- Base\Enqueue
- Pages\Admin
- Base\SettingsLinks
inc_a101518/Api/SettingsApi.php
- This class supports the class inc_a101518/Pages/Admin.php
- Accepts variables on admin page, admin subpages, option settings passed from Pages/Admin.php and stores them as class properties.
- The register( ) function defines the add_actions for adding the Admin pages at the ‘admin_menu’ hook and adding Option settings at the ‘admin_init’ hook.
inc_a101518/Api/Callbacks/AdminCallbacks.php
- This class supports the class inc_a101518/Pages/Admin.php
- The functions return the html markups templates for the admin page and subpages.
inc_a101518/Base/Activate.php
- Execute during plugin activation.
- It is registered by the main plugin file.
inc_a101518/Base/Deactivate.php
- Execute during plugin deactivation.
- It is registered by the main plugin file.
inc_a101518/Base/BaseController.php
- Extract the plugin_path, plugin_url and main plugin file names and stores them to public variables of the class.
inc_a101518/Base/Enqueue.php
- Provide functions to enqueue the CSS and JS files for Admin and frontend.
inc_a101518/Base/SettingsLinks.php
- Provide functions to create custom links for the plugin on the Plugin List page.
inc_a101518/Output/Ivr__m_m_Output.php
- Contains functions for delivering the shortcode output to the frontend.
inc_a101518/Pages/Admin.php
- The settings/parameters for creating the admin page, subpages, and option fields are defined within the functions of this class.
- The regster( ) function is triggered by Init->register_services() to actually create the admin pages.
inc_a101518/templates (Folder)
- This folder contains the html markup templates for the admin page and subpages.
vendors (Folder)
- This folder is created by Composer. No need to touch it.
Version History & Download
| Ver. | Rel Date | Remark |
| 1.0.0 | 2021-10-23 | First release |
Reference
- Create a WordPress Plugin from Scratch by Alessandro Castellani (2017) – Parts 1 to 18
- PHP – Autoload classes using Composer by TFW (2021)
- WordPress uninstall.php file – The Complete Guide by Jeff Starr (2020)
