Create Your Own WordPress Plugins

On March 5, 2013, in PHP, Programming, WordPress, by LuCuS

WordPress PluginRecently, I finished reviewing a book that discusses how to generate passive income online. One of the topics touched on in that book mentioned that it is possible to generate extra income using WordPress and more specifically creating plugins for WordPress. Although the book didn’t go into the details about how to create plugins, it did state that it is extremely easy. So, being curious, I decided to find out for myself just how easy it is to create my own WordPress plugins. In the end, the author was correct. Creating your own WordPress plugins is extremely simple to do and now I want to take a few minutes to show you how to create your own WordPress plugins. Before continuing, it is advised that you know how to write PHP.

For my plugin, I decided I wanted the ability to add a message at the end of every post and I wanted to be able to change this message using the admin console. To do this, you will first need to create a new directory which will house your plugin. Inside your WordPress installation, you will see a folder called “wp-content”. Change directory into that folder and there you will see another folder called “plugins”. Change directory into that folder. Once there, you will need to create a new folder. For my plugin, I decided to name it “Post Followup”, therefore I created a new folder called “post-followup”.

After you have created your new plugin’s directory, change directory into that folder. Once there, you will need to create a new PHP file. For mine, I named it “followupplugin.php”. However, I don’t think the naming convention really matters. Next, open the new file with a text editor. In order for a WordPress plugin to be picked up by WordPress, there is basically only one line needed. This line is a metadata line which contains the name of your plugin. However, it is recommended that you include as much metadata about your plugin as possible. This is especially helpful when sharing your plugin with others. Here is the metadata I used for my Post Followup plugin.

<?php
/*
Plugin Name: Post Followup
Plugin URI: http://www.prodigyproductionsllc.com
Description: Add a followup message at the end of every post.
Version: 1.0
Author: Lucus Darnell
Author URI: http://www.prodigyproductionsllc.com
License: GPL2
*/
?>

Now that you have a working plugin, it is time to add some code to make it actually do something. For that, we will begin by telling WordPress that we want to insert our plugin by calling the “add_filter”. According to the WordPress docs, filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. The “add_filter” function accepts a few parameters, but we are only concerned with the first two at this time. The first argument is the tag name of the filter to hook your function into. Since we will be working with posts, we will need the “the_content” tag. The second argument is the name of the function we want to execute.

add_filter(‘the_content’, ‘post_followup’);

We will look into our “post_followup” function later. Before that, we need to setup a few things for our plugin to work with. Since we want the ability to modify our followup message from the admin console, we will need to first create a new option to store our message in. For that, we will want to call the “add_option” function. Options are key / value pairs that get stored in the database and can be called from any function at any time. Since we want our option to be created when the plugin is first installed, we will need to register an activation hook like so:

register_activation_hook(__FILE__, ‘post_followup_install’);
function post_followup_install() {
add_option(‘post_followup_message’, ”, ”, ‘yes’);
}

The first argument for our “add_option” function is the name of the variable you are creating. The second argument is a mixed value you want to use as the default value. The third argument tells WordPress whether or not this option has been deprecated or not. The last argument tells WordPress whether or not this option should be auto loaded. You can find more information about the add_option function here.

Since we are creating a new option when the plugin gets installed, it is only right that we also remove the option whenever the plugin is uninstalled. This will help keep our WordPress database nice and tidy. To do that, we will need to register a deactivation hook at which time we will call the “delete_option” function, passing it only the name of the option we want to remove.

register_deactivation_hook(__FILE__, ‘post_followup_remove’);
function post_followup_remove() {
delete_option(‘post_followup_message’);
}

Next, we will need to have WordPress add a new menu item which will take us to our plugin’s settings page when clicked. To do that, we will first want to make sure that the currently logged in user has admin access. If the user does have admin access (or whatever role you decide to allow to change the followup messages), you will need to call the “add_action” function. The add_action function accepts 2 parameters. The first parameter is the name of the action we want our action to be triggered on, “admin_menu” in our case. The second parameter is the name of the function we want to add. In our example, we want to add a new action called “post_followup_admin_menu”. We will also need to go ahead and add our post_followup_admin_menu function in which we will need to add a call to “add_options_page” which will display our settings page.

The add_options_page function accepts 4 required arguments and 1 optional argument, they are page title, menu title, capability, menu slug, and callback function. Even though the fifth parameter is optional, we will still be using since this is where we will construct our actual settings page. For the purposes of this plugin, I have chosen to call my optional callback “post_followup_html_page” which we will examine next. But, before we do, let’s take a look at what our admin check and action & options page registrations should look like.

if (is_admin()) {
add_action(‘admin_menu’, ‘post_followup_admin_menu’);
function post_followup_admin_menu() {
add_options_page(‘Post Followup Options’, ‘Post Followup’, ‘administrator’, ‘post-followup’, ‘post_followup_html_page’);
}
}

As just mentioned, we will now need to create a new function that will display our settings page. I’m not going to go thru all of the code with you as I’m assuming you should know what a simple HTML form looks like. Instead, I’m just going to show you the code so that we can move on to the final pieces of the plugin.

function post_followup_html_page() { ?>

<div>
<h2>Post Followup Options</h2>
<p><em>Add a followup message at the end of every post.</em></p>

<form method=”POST” action=”options.php”>
<?php wp_nonce_field(‘update-options’); ?>
<p>Enter your “Post Followup” message.</p>
<textarea name=”post_followup_message” type=”text” id=”post_followup_message” rows=”10″ cols=”80″><?php echo get_option(‘post_followup_message’); ?></textarea><br/><br/>

<input type=”hidden” name=”action” value=”update” />
<input type=”hidden” name=”page_options” value=”post_followup_message” />
<br/>
<button type=”submit”><?php _e(‘Save Changes’) ?></button>
</form>

<br/><br/>
<h3>Learn More</h3>
<p><a href=”http://www.prodigyproductionsllc.com” target=”_blank”>Read the post</a> that started this plugin.</p>
</div>

<?php }

Alrighty, the last piece of the plugin we will need to code will be the function that we defined in our add_filter at the beginning of this article, “post_followup”. Since we want our followup message to only appear at the end of the actual articles (not on the article list pages, category pages, etc…), we will want to first make a call to “is_single()” which lets us know that we are on a single post page. If we are, we can get message from our settings form above by calling the “get_option” function, passing it the same option name we used both in our settings form as well as in our “add_option” call earlier, “post_followup_message”. Once we have our followup message, we will add it to the rest of the page by appending it to the content argument that we pass into our “post_followup” method and return the concatenated text.

function post_followup($content = ”) {
if (is_single()) {
$followup_message = get_option(‘post_followup_message’);

$text = $content . $followup_message;
return $text;
}
else
return $content;
}

That’s it. You now have everything you need for your plugin to work. The only things you have left to do now are to activate your plugin and add the message you want to appear at the end of every post. To do that, log into your WordPress site as an admin. Once you are logged in, click on the “Plugins” link on the left. Scroll down until you see your plugin and click the “Activate” link under the plugin name. If you do not see your plugin in the list, it’s probably because you made a mistake somewhere in your plugin code. So, go back and double check that everything is correct and that there are no typos or missing brackets or tags.

Activate WordPress Plugin

Once you have activated your plugin, hover your mouse over the “Settings” menu so that you can see its sub-menu. If you see your plugin in the sub-menu, you can click on it to go to the settings page you created earlier. Type in the message you want displayed after every post and click the “Save Changes” button.

Post Followup WordPress Plugin Settings

With your message saved, go to any of your posts and scroll to the bottom. There you should see your message. You will see the same message on every post page you click on. Keep in mind that you can store HTML in your message, allowing you to style it or to add other things such as Javascript. If you look at the end of this article, you will see that I used this plugin to add a message that reads “If you enjoyed this post, please consider making a donation” along with a PayPal donate button. Below is the code I used to make this plugin. Feel free to modify it to do other things. Just make sure you come back here and tell the rest of us what kinds of cool plugins you created using your new found knowledge.

<?php
   /*
   Plugin Name: Post Followup
   Plugin URI: http://www.prodigyproductionsllc.com
   Description: Add a followup message at the end of every post.
   Version: 1.0
   Author: Lucus Darnell
   Author URI: http://www.prodigyproductionsllc.com
   License: GPL2
   */

   add_filter('the_content', 'post_followup');

   register_activation_hook(__FILE__, 'post_followup_install');
   function post_followup_install() {
      add_option('post_followup_message', '', '', 'yes');
   }

   register_deactivation_hook(__FILE__, 'post_followup_remove');
   function post_followup_remove() {
      delete_option('post_followup_message');
   }

   if (is_admin()) {
      add_action('admin_menu', 'post_followup_admin_menu');
      function post_followup_admin_menu() {
         add_options_page('Post Followup Options', 'Post Followup', 'administrator', 'post-followup', 'post_followup_html_page');
      }
   }

   function post_followup_html_page() { ?>

	 <div class="wrap">
	   <h2>Post Followup Options</h2>
	   <p><em>Add a followup message at the end of every post.</em></p>

	   <form method="POST" action="options.php">
	      <?php wp_nonce_field('update-options'); ?>
	      <p>Enter your "Post Followup" message.</p>
	      <textarea name="post_followup_message" type="text" id="post_followup_message" rows="10" cols="80"><?php echo get_option('post_followup_message'); ?></textarea><br/><br/>

	      <input type="hidden" name="action" value="update" />
	      <input type="hidden" name="page_options" value="post_followup_message" />
	      <br/>
	      <button type="submit" class="button-primary"><?php _e('Save Changes') ?></button>
	   </form>

	   <br/><br/>
	   <h3>Learn More</h3>
	   <p><a href="http://www.prodigyproductionsllc.com/articles/programming/create-your-own-wordpress-plugins/" target="_blank">Read the post</a> that started this plugin.</p>
	 </div>

   <?php }

   function post_followup($content = '') {
      if (is_single()) {
		 $followup_message = get_option('post_followup_message');
	  
         $text = $content . $followup_message;
		 return $text;
      }
      else
         return $content;
   }
?>

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.
$2.00
$5.00
Other

Related Posts

Tagged with:  

Leave a Reply