Loading...

Exploring Wordpress APIs: Plugins

This is the first in a series of posts involving the many APIs of WordPress. Similar to Content Management Systems/Frameworks like Drupal, Wordpress has APIs where a programmer can interact with and change the default functionality of the CMS. It is through systems like this that platforms like WordPress become easily extendable.

In this series of posts, we will discuss each API listed on https://codex.wordpress.org/WordPress_APIs. With each one, we will discuss what it is and what it's for (including a "physical" example of the subject of the API if applicable), when and where it is meant to or can be used, why use it (as opposed to something else), and how does it work. Along with that, we will provide a working example of a custom plugin that keeps track of Ping-Pong player stats.

Before moving forward, since this will be mentioned a lot, let us briefly define an API. An API or Application Programming Interface, as defined by Wikipedia, "is a set of clearly defined methods of communication between various software components." https://en.wikipedia.org/wiki/Application_programming_interface. It provides an easier way for a Programmer to Interact with, in this case, a particular system, platform, framework, etc. It also in itself sets rules as to how the subject should be interacted with. WordPress through its APIs allows us to easily interact with it in a standardized safe way so that we can make it into what we want it to be for our project.
 

Plugin API

We'll start with the Plugin API as this is arguably one of the most important and a good starting point seeing how it ties into the creation of plugins which allow us to extend or alter WordPress.

What's a Plugin?

A Plugin provides a means for altering WordPress' default functionality. If you want to add functionality to your WordPress site, create a plugin. If you have experience with another CMS like Drupal, for example, you are more than likely familiar with this concept in the form of Drupal modules. Many things you can put in a plugin, you can also add to a custom theme in WordPress.

The Plugin API gives you an easy structured way to build a plugin.

When and why should a Plugin be used?

You'd create one anytime you'd want to extend WordPress' functionality. As I mentioned before, much, if not all, of the code you put in a plugin, can be put into a custom theme, perhaps in the functions.php file.

Where can I use it?

All Wordpress plugins are stored in the wp-content/plugins directory. The only thing we need for a basic working plugin is a PHP file. You can add a file called ourplugin.php in a directory called ourplugin.

How does this work?

The main feature of the Plugin API is the hooks. WordPress has two types of hooks, actions and filters. Actions allow you to have your custom code run when an event happens in WordPress, whether that event is from WordPress core or another plugin. An example would be the "admin_notices" action for when notices for admin pages are loaded.

function score() {
  echo "Score!!";
}
add_action('admin_notices', 'score');

Dashboard image -  Exploring Wordpress APIs: Plugins

 

Using the add_action function, one of the 8 action hook functions, I can have my code run once admin notifications are loaded allowing me to, for example, post a custom message. Score!!

The other hook type, Filters, allows you to make changes to the output of WordPress (on the front or back end) with your custom code. The example we'll use is with the "widget_title" filter which allows us to have control over the title displayed for a widget. We'll leverage the score function from the previous example.

add_action('widget_title', 'score');

Recent Posts image - Exploring Wordpress APIs: PluginsHello World image - Exploring Wordpress APIs: Plugins

 

Leveraging a variety of hooks, we are able to have our custom code operate anywhere in WordPress.

 

Plugin Setup

Lets set up our Ping-Pong Stats plugin. First, create the ping_pong_stats directory in the plugins directory. Next, add a PHP file of the same name. In this file, add some lines of metadata that tells WordPress about our plugin.

<?php 

/* 
Plugin Name: Ping-Pong Stats 
Description: This is a plugin that tracks Ping-Pong statistics. 
Author: Sirod Johnson 
Version: 0.1 
*/

 

WordPress will now recognize our plugin. After installing it, create a custom post type. Review the documentation on Post Types for more information. We will leverage an action hook called init. This hook is fired once most of WordPress is loaded. This will allow us to add our custom post type after items such as theme actions have been loaded.

First, let us add our action.

add_action( 'init', 'ping_pong_create_post_types' ); 

function ping_pong_create_post_types() { 
}

Here we created our function ping_pong_create_post_types which was added as an argument in the add_action function. Now we can add our custom post type code inside our ping_pong_create_post_types function.

$labels = array( 
  'name'           => 'Games', 
  'singular_name'  => 'Game', 
  'menu_name'      => 'Games', 
  'name_admin_bar' => 'Game', 
  'all_items'      => 'All Games', 
  'add_new'        => 'Log Game', 
  'add_new_item'   => 'Log Game', 
  'new_item'       => 'New Game', 
  'view_item'      => 'View Game Stats', 
); 

$args = array( 
  'labels'      => $labels, 
  'public'      => true, 
  'has_archive' => true, 
  'supports'    => array( 'title', 'editor', 'author', 'comments' ), 
); 

register_post_type( 'ping_pong', $args );

Now if we reload our dashboard, we will see our new post type loading in.
Reloaded dashboard image -  Exploring Wordpress APIs: Plugins

 

That's it for Now!

Throughout this series, we will continue to build on our plugin with the new things we learn from the APIs we explore. Hopefully, this series will allow you to get the most out of your WordPress projects or perhaps help you gain a better understanding of various areas. I hope to do the same. As you explore with me, additional information as well as feedback through comments is appreciated.

I look forward to exploring the Dashboard Widgets API next.