Creating a WordPress Plugin

There are numerous WordPress plugins available to help you achieve the functionality that you require on your WordPress-driven web site, but if you can’t find what you’re looking for you may want to consider building your own plugin. This way you can be sure that it does exactly what you want and doesn’t do anything you don’t. Here are some easy to follow instructions and tips that will help you on your way.

File Headers

The first step when creating a WordPress plugin is to create the plugin file and set the necessary headers:

Plugin Name: Agent Testimonial
Description: Agents will be given permission to add/edit/view/order/search
Version: 0.4
Author: Blair Venn
Author URI:
License: A "Slug" license name e.g. GPL2

The plugin file can either be stored in “wp-content/plugins” directly or in it’s own sub-folder under this folder. WordPress will automatically detect the plugin and display it based on the information in the header:

Create Database Structure

In the plugin file you can create an install function to handle the creation of any database tables:

function agent_testimonial_install() {
    global $agent_testimonial_version;
    global $table_prefix;
    global $wpdb;

    $table_name = $table_prefix . 'agent_testimonial';

Specify the table structure in the form of an SQL CREATE TABLE statement:

if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
        $sql = "CREATE TABLE " . $table_name . " (
                field INT,
                PRIMARY KEY  (id)

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        add_option('agent_testimonial_version', $agent_testimonial_version);

Modifying Existing Database Structure

If you are modifying an existing plugin and need to change the table structure, this is also handled by the same install function as above:

if ($installed_ver != $agent_testimonial_version) {
        $sql = "CREATE TABLE " . $table_name . " (
                field INT,
                PRIMARY KEY  (id)

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        update_option('agent_testimonial_version', $agent_testimonial_version);

Remember that you will also need to update the creation query to reflect the changes. You will also need to update the version number. To use the new version, deactivate the plugin and then reactivate it. On doing this, WordPress will apply the table structure changes to the database without any loss of data.


You need to register the installation function:

register_activation_hook(__FILE__, 'agent_testimonial_install');

add a menu item:

add_action('admin_menu', 'agent_testimonial_menu');

function agent_testimonial_menu() {
    add_dashboard_page('Agent Testimonials', 'Testimonials',
            'manage_agent_testimonials', 'agent-testimonial-manage',

and create the screen:

function agent_testimonial_manage() {
    if (!current_user_can('manage_agent_testimonials')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));

    // Some HTML here

Congratulations! You have now created a WordPress plugin.

Designing Screens

For some tips on designing screens I found the following link useful:

How to design and style your WordPress plugin admin panel.

Adding Pages Without Menu Items

Assuming that you will want an add/update screen and maybe a view screen, you will need extra pages that don’t require their own menu items.

To do this you must add the action as above:

add_action('admin_menu', 'agent_testimonial_add_page');

and implement the function:

function agent_testimonial_add_page() {
    $hookname = get_plugin_page_hookname('agent-testimonial-add', 'index.php');
    add_filter($hookname, 'agent_testimonial_add');
    global $_registered_pages;
    $_registered_pages[$hookname] = true;
    $url = admin_url('index.php' . '?page=' . 'agent-testimonial-add');
    return $url;

Note how this function differs from the example above. The difference is that this function does not create a menu item for the page.

Then you just need to create the function to display the page:

function agent_testimonial_add() {
    // HTML etc


You might want to create a custom role for the plugin user. This is done in the theme file located at “wp-content/themes/theme-name/functions.php”:

add_role('agent', 'Agent', array(
        'read' => true

Then you need to add capabilities for the role (otherwise the user won’t be able to do anything):

$role =& get_role('agent');

To use these capabilities, add the following to the appropriate function:

if (!current_user_can('add_agent_testimonial')) {
    wp_die(__('You do not have sufficient permissions to access this page.'));


If the new functionality you require is fairly basic you may find that a complete plugin with it’s own database tables is a bit of overkill. If this is the case, you may be better off using Options. Options are much simpler and storing/updating is handled automatically.

Options are appropriate if:

  • the data for the plugin is unlikely to change regularly
  • a single item of data is required rather than rows and rows of data

As you can see, creating a plugin in WordPress is very simple and easy to do. You just need to create a file with the appropriate headers, implement an installation function and a function for each page then remember to add actions, capabilities and roles so that each page can be accessed.

Leave a Reply

Your email address will not be published.

Scroll to top