WordPress plugin development basics

WordPress LogoWordPress is by far the most widely used Content Management System (CMS) for websites on the World Wide Web. WordPress plugins allow you to extend the core functionality provided by WordPress.

A WordPress plugin works by “hooking” into the WordPress core. It does this via actions and filters:

  • Actions allow you to execute plugin code in places where WordPress core code is being executed. So for example you can display a  message in the admin area by attaching your code to the admin_notices action.
  • Filters allow you to filter data that is being handled by the WordPress core. For example you can use the the_content filter to filter a post or page’s content.

WordPress provides a variety of actions and filters that a plugin can hook into. The full documentation on these is provided on the WordPress codex filter reference and action reference.

Coding a WordPress plugin

A WordPress plugin is installed in [wordpress root]/wp-content/plugins. It’s best to give the plugin its own sub directory in the plugins directory. So for example you could create a sub directory called foobar in wp-content/plugins.

A plugin file is a PHP file with a comment header like the following:

<?php
/*
Plugin Name: FooBar Plugin
Plugin URI: //abdussamad.com
Description: Displays a notice in the admin area
*/

Only the main plugin file needs to include the above information.

You can include additional info like license and version too. WordPress will parse these comments and display them in the administration area.

WordPress plugins are included in the global scope so they need to have unique function/class names. So it is a good idea to include all your plugin code inside its own class:

<?php
class foobar_plugin {
 
}
 
new foobar_plugin();

Let’s hook into the admin_notices action and display a message in the administration area:

<?php
class foobar_plugin {
  function __construct() {
    add_action( 'admin_notices', array( $this, 'display_notice' ) );
  }
 
  public function display_notice() {
    ?>
     <div class='updated'>
       <p>
          <strong>Hello World!</strong>
       </p>
     </div>
 
  <?php
  }
}
 
new foobar_plugin();

When this plugin is activated an object of foobar_plugin class is created. That results in the constructor function being executed. The constructor uses the WordPress add_action function to plug into the WordPress core.

The first argument to the add_action function is the action name admin_notices. Then we provide the name of the callback function. Because the callback function is a class method the second argument is an array with the object as index 0 and the function name as index 1.

The display_notice public function is responsible for displaying the admin notice. The message “Hello World!” is wrapped in a suitable div so that it is displayed prominently in the admin section.

When you activate this plugin you should see a message like this on your admin panel:

WordPress Plugin FooBar

This is just a simple example of a WordPress plugin. WordPress plugins can do some pretty neat things. They can be very powerful indeed!

Leave a Reply

Your email address will not be published.