wordpress plugin scaffolding from scratch

Building a wordpress plugin scaffolding from scratch

This is a basic walkthrough on how to create the scaffolding for a wordpress plugin from scratch.

Firstly, create a repo in github
clone that repo to your machine and inside, create a file with the same name as the repo:

coolplugin/coolplugin.php

inside this file, add your plugin header information.

  /*
  Plugin name: coolplugin
  Plugin URI: http://coolplugin.com/
  Description: Description about coolplugin 
  Author: AUTHOR_NAME
  Author URI: http://coolplugin.com
  Version: 0.1
  */

Commit this to your repo :)

Next, create a couple of bootstrapping folders:

includes
assets
assets/css
assets/js
vendor
views

These can store all of your custom code (in the includes and views folders), all of your css and javascript (the assets folder) and any dependancies from composer (the vendor folder)

Add the following to automatically include and autoload files from the vendor folder:

if (file_exists(WP_PLUGIN_DIR . '/coolplugin/vendor/autoload.php')) {
    require_once WP_PLUGIN_DIR . '/coolplugin/vendor/autoload.php';
}

Next, register some activation, deactivation, uninstall and init hooks:

register_activation_hook( __FILE__, 'coolplugin_activate' );
function coolplugin_activate() {
    //code 
}

register_deactivation_hook( __FILE__, 'coolplugin_deactivate' );
function coolplugin_deactivate() {
    //code
}

register_uninstall_hook( __FILE__, 'coolplugin_uninstall' );
function coolplugin_uninstall() {
    //code 
}

add_action( 'init', 'coolplugin_dostuff' );
function coolplugin_dostuff(){
    //code
}

add_action('admin_menu', 'coolplugin_admin_stuff');
function coolplugin_admin_stuff(){
    //code
}

From here, you can add menu items and admin pages:
https://codex.wordpress.org/Adding_Administration_Menus

Starting with an admin page, the code needed to register the page is:

// place this code inside the coolplugins_admin_stuff() function
add_action('admin_menu', 'coolplugin_admin_stuff');
function coolplugin_admin_stuff(){
    add_options_page("CoolPlugin Admin Page", "CoolPlugin Admin Page", 1, "CoolPlugin Admin Page", "coolplugin_admin");
    }
}

Next, we need to create the page itself (we only have a menu item so far), we registered a function coolplugin_admin which will load when the link in the menu is clicked

At this point, its a good point to refactor – we have a lot of code that is relating to an admin area that can be offloaded to the includes section;

Inside the includes section, create a file called coolplugin_admin.php
and inside that, move the last block of code we wrote:

// move this code inside the includes/coolplugin_admin.php file
add_action('admin_menu', 'coolplugin_admin_stuff');
function coolplugin_admin_stuff(){
    add_options_page("CoolPlugin Admin Page", "CoolPlugin Admin Page", 1, "CoolPlugin Admin Page", "coolplugin_admin_view");
    }
}

// add this new code
function coolplugin_admin_view(){
    // include admin view
    if (file_exists(WP_PLUGIN_DIR . '/coolplugin/views/coolplugin_admin_view.php')) {
        require_once WP_PLUGIN_DIR . '/coolplugin/views/coolplugin_admin_view.php';
    }
}

Then inside the main coolplugin.php file, add this, below the lines to include the autoloader:

// include admin page
if (file_exists(WP_PLUGIN_DIR . '/coolplugin/includes/coolplugin_admin.php')) {
    require_once WP_PLUGIN_DIR . '/coolplugin/includes/coolplugin_admin.php';
}

Now we can move on and add the admin page code itself:

Create a file inside the views folder, called coolplugin_admin_view.php

At this stage you can upload the whole thing and activate, and you have the scaffolding to build your plugin – from this stage you should be good with the codex to create your plugin features

All the code for this wordpress plugin scaffolding is available under GPL in my github, here: https://github.com/localhost8080/coolplugin

If you got to here, you might want to check out my WordPress security post..

Leave a Reply