WordPress Checklist – Custom Plugins

How to create a plugin

Creating a plugin can be daunting but before you proceed ask yourself the following questions:

  1. Do I need a plugin?
  2. Can it be solved with a function in your themes functions.php file?
  3. Are there any 3rd party plugins? If yes, is it reliable?

If you are still sure you still need one you can now proceed.

Folder Structure

Create a folder locally on your desktop called ‘my-plugin-name’, inside it, create a php file and copy and paste the below.

/* 
Plugin Name: MY PLUGIN NAME
Plugin URI: http://www.piercecommunications.co.uk
Description: WHAT DOES IT DO??
Author: ME
Version: 1.0
Author URI: http://www.piercecommunications.co.uk 
*/

    ################################################
    #
    #  Add Menu Links to Sidebar in CMS
    #
    ################################################

    add_action("admin_menu", "wp_plugin_status");

    function wp_plugin_status() {

        // Menu name
        add_menu_page("PLUGIN NAME", "Plugin Name", 0, "plugin-name", "plugin_name", PLUGIN_PATH."menu_icon.png");

        // Page 1
        add_submenu_page("plugin-name", "Page 1", "Custom Scripts", 0, "page-1", "page_1_function");

        // Page 2
        add_submenu_page("plugin-name", "Page 2", "Custom Styles", 0, "page-2", "page_2_function");

        // Hide the first Subpage
        remove_submenu_page('asset-manager','asset-manager');

    }

    function page_1_function(){

        // show html for page 1
        include('views/page_1_html.php');

    }

    function page_2_function(){

        // show html for page 2
        include('views/page_2_html.php');

    }

Save the file as ‘index.php’

Upload the folder to your website:

www.mywebsite.com/wp-content/plugins/

Login to the CMS and go to the plugins section.

plugin-screen

Activate the plugin and a new menu will appear in the sidebar. You can now add whatever you like within these pages that fit your purpose.

plugin-menu

Note that there are many ways to build plugins and you don’t necessary need to create a menu with multiple pages.

Security

A simple yet effective security process below will prevent users accessing the plugin via its full directory structure: eg http://www.mysite.com/wp-content/plugins/my-plugin-name/

/* 
Plugin Name: MY PLUGIN NAME
Plugin URI: http://www.piercecommunications.co.uk
Description: WHAT DOES IT DO??
Author: ME
Version: 1.0
Author URI: http://www.piercecommunications.co.uk 
*/

define('PLUGIN_PATH',plugin_dir_url(__FILE__));
define('CURRENT_USER_PATH','http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'/');

if (CURRENT_USER_PATH != PLUGIN_PATH){

    // PLUGIN CODE

} else {

    die(); //literally

}

Using the above code means the only way to access the plugin is via the CMS and you need login credentials to access it.

Case Study: WP Asset Manager

I recently built a custom plugin called WP Asset Manager which required WP hooks and MYSQL calls.

The code is on github so pick through the code to see the reasoning and functionality behind the plugin