WordPress Plugin Development for Beginners

You want to venture into WordPress plugin development but do not know exactly what to do or where to start. If this describes you, then you are precisely who we have made this tutorial for. Here, we will go over the bare bones of developing your own WordPress plugin. Along the way, we will provide tips on how to make sure your practices are going smooth.

Exactly What is a WordPress Plugin?

In a way, plugins are what make WordPress the largest content management system there is. These are modules which  provide an infinite array of functions to your website. These functions could be as simple as a clock widget, to as complicated as eCommerce integration to your website.

The functionality of WordPress plugins differ from your theme. In contrast. they work independently, powered by short-code, custom-code, hooks, filters, and widgets to do their tasks.

The Advantages and Drawbacks of WordPress Plugin Development

Undoubtedly, plugins are little gems that glisten up your website. However, there are some conflicts with a plugin when it comes down to the technical aspects. Let us take a closer look at both the pros and cons.

Advantages

  1. A plugin plays as a container for your code and elements. In a theme, building similar functions may mix your elements up. On the other hand, with a plugin, all functioning parts are located in a single package.
  2. Plugins are compatible with nearly any theme. They were created to be modular, meaning you can simply cppy it into a new project.
  3. The code is more versatile since a plugin can be created in an Object Oriented fashion.
  4. They utilize WordPress’s action and filter hooks, making them easy to develop.

Drawbacks

  1. Plugins do not have a lot of design modification. Their sole purpose is to provide functionality, while themes take care of the design end.
  2. Plugins may conflict with one another or overlap. At times, the functionality of one plugin may negatively impact the functionality of another plugin.
  3. There is little access to a website’s theme for a plugin. They cannot directly output code to where the theme is located. Instead, the theme must allocate an area for your plugin to do this.

Core Components of Plugins

Now, there are a list of key aspects when it comes to plugin development. If you have experience in developing WP themes, then this may ring a bell. Nonetheless, you need to understand how these elements apply to plugins in order to start off correctly.

Action Hooks in WordPress Plugin Development

An activity which WordPress performs at a particular time is known as an  action hook.  Triggering this action will locate all functions attached to it and execute them in order.

Each WordPress action has a name (tag) based on its function.

Example:

The action save_post will be called when WordPress is saving a form of content.

The do_action() function defines all actions. It composes of the following parameters:

  1. $tag (mandatory)
  2. $args (optional, one or more variables)

To summarize, each action will have its name and a number of extra variables if desired.

Save_post Continued

The save_post action hook takes in 4 values.  The name (tag) is the first one and the other three are extra variables containing the following:

  1. ID of the post
  2. The post object
  3. Whether the post is live or being updated
//the save_post action hook
do_action(`save_post`, $post_ID, $post, $update);

Hooking onto this action will enable you to move onto other tasks once a post is saved.

Develop your Own Actions

WordPress contains several actions at its core, but you have the power to develop your own. Doing so will enable you to easily call on your function when required throughout your plugin.

We will create a new action using  the do_action function. This action will be called right after the title on single pages.

//create a new action to be used after our page title
do_action('page_subtitle', $post);

When one single page is displayed it will show the title as normal.  Later, it hits the page_subtitle action. WordPress will check for all functions hooked to this action. After doing so, it will execute these functions.

Call your Action, Execute your Function

Calling a particular function requires you to attach your function. For this, you use add_action()

Then, this takes in the following parameters:

  1. $hook
  2. $function_to_add
  3. $priority
  4. $accepted_args

The first two are required, while the last two are optional.

 

Use the Required Arguments

Supply the hook name and function to call (both mandatory values) to call the add_action function.

 

//when 'save_post' is triggered, call the saving function
add_action('save_post','save_my_custom_post');
//defines the function that will handle your saving
function save_my_custom_post($post_id){
    //you will save here
}

As shown, we attach the  save_my_custom_post function when the save_post action takes place so that we can move on to another task after the work is saved.

Priority and accepted_args Value

The mandatory values may not always suffice in the WordPress plugin development.

You might be required to set the values  $priority and $accepted_args manually to put your action in action.

The priority of the action decides in which order the functions will take place. By default, the value of 10 will be used for the priority of actions. We can increase or decrease this value to execute your function earlier or later on. When your plugins are using the same action hook, this component is key. This is because it allows you to specify at which points your function is to run.

The accepted_args equals  how many variables are being passed in the add_action call. By default, an action accepts one single argument. However, you will need the save_post action if your action takes in more than one variable.

//the save post action, according to WordPress
do_action( 'save_post', $post_ID, $post, $update );

//add  function onto  'save_post' hook, supplying priority and args
add_action('save_post','save_my_page_extended,10,3);

//function executed on save (for all three variables)
function save_my_page_extended($post_ID, $post, $update){
    //access every variable to do some work
}

Numerous variables can pass to the save_post action it. Therefore, we need to set its priority and  how many variables it will be passing. Upon doing so, we can add the variables into our function and access them.

Filter Hooks in WordPress Plugin Development

The next component of WordPress plugin development is a filter.This is a hook on WordPress which accepts a variable or series of variables, modifies them, and then returns them back. The main use of these filters is to modify default info.

WordPress comes bundled with several filters. We create them using the apply_filters() function. This function takes in these arguments:

  1. $tag: The name of the filter(mandatory)
  2. $value: The variable that will be filtered(mandatory)
  3. $var: Used to pass additional values (optional)

Plugins usually use these filters so that you can modify elements without having to modify the source.

A Basic WordPress Filter

The get_the_excerpt filter  is a filter  you use inside the posts loop to gain access to the excerpt.

It is part of WordPress’s core. This filter solely defines the name of the filter and the function to call (mandatory arguments). It cannot define additional (optional) arguments.

//get the except for a post, as defined in /wp-includes/post-template.php
echo apply_filters( 'the_excerpt', get_the_excerpt() );

You have the access to attach your own function to the the_excerpt filter. Then, you could modify the excerpt before you return it.

Create a Filter

Consider the following example. If you are generating an array of names in a plugin, we can create a filter system for these names by using the apply_filters hook. By doing so, you are instructing WordPress that any function which hooks onto these values can modify them.

//make name value array filterable
$name_values = apply_filters('filter_name_array_values',
            array('Thomas','Hannah')
); 
Such a filter will let you or other developers modify these values dynamically.

Execute Filters

Use add_filter()function to execute your filters, which takes in these values:

  1. $tag (mandatory)
  2. $function_to_add (mandatory)
  3. $priority (optional)
  4. $accepted_args (optional but mandatory for more than one passed argument)

With the add_filter function, you define the name of the filter to call alongside the function to call.  Here is how we can use this to filter the list of names we created in our previous example.

//Change the saved name values by filtering them
function add_new_names($names){
    $names[] = 'Hannah';
    return $names;
}
add_filter('filter_name_array_values','add_new_names');

Here, we attach the add_new_names function to the filter_name_array_valuesfilter and take in one of the $namesvariables. We had originally commanded that the default values be an array of Thomas and Hannah.

The function will take in the existing values. Additionally, it will provide a new value onto the end called Hannah.

Lastly, return the values. This step is crucial, because all additional functions attached to the filter will need to take in these same values.

Shortcodes in WordPress Plugin Development

In general, your plugin will not have access to your theme files. Hence, you need a method of displaying your custom function to the end user all the while making it easily modifiable by your site admin.

Shortcodes enable you to develop complex functionality which is modifiable via values and attributes. We add shortcodes to the visual editor. When your users view your site, the code is executed.

Shortcodes are perfect way to do this as they allow developers to create complex functionality that can be modified with simple values and attributes. These shortcodes are added to the visual editor (or other locations) and when viewed by your users they execute your code.

Check out how to create shortcodes here.

Widgets in WordPress Plugin Development

Widgets are another method of executing the code of your plugins and offering a convenient interface at the same time.

Here is an example of widget configuration:
WordPress Widget

Widgets are similar to shortcodes in that they define options for its output in the back-end then display the results on the front-end.
With this basic understanding of how to develop WordPress plugins, we hope you can build and start creating plugins tailored to you and your customer base. For some inspiration, here are our 8 essential WordPress plugins. Happy developing!

Leave a Reply

Your email address will not be published. Required fields are marked *