Creating a Plugin

 

I’m going to dive right into this blog post on creating a plugin for WordPress. There are countless tutorials out there and ways to code and structure a plugin, but for the purposes of these articles I’m going to make it short and simple.

To start off with, you’ll need to know a bit about WordPress – it’s a very capable content management system with tons of extensible options and countless thousands of paid and free plugins. Plugins are really very simple and are broken down into three main components:

  1. The information header that describes the plugin and details about the author
  2. The user defined functions that run when the plugin is triggered
  3. The add_action() and/or add_filter() functions that connect it to WordPress

For an extremely simple plugin, lets look a basic one that uses a WordPress action and stores a simple variable that can be sent as an output to the template.

Connect via FTP to your website, browse to /wp-content/plugins and create a folder inside your WordPress plugins folder called “test”. Inside the test folder, create a blank file called “test.php” and insert the following lines of code into it:

 

This is the information header – it’s required in order for WordPress to properly display the plugin and for WordPress to recognize it. Once you’ve done this you have officially created your first plugin that does absolutely nothing! Now you just need to activate it inside the plugins section in your admin dashboard, and we can continue.

1

So here is where things get interesting – we want to perform an action within WordPress using a user defined function (which means you are creating your own function and calling it). The WordPress Codex has a full reference for Plugin actions here: https://codex.wordpress.org/Plugin_API/Action_Reference

Let’s select the wp_footer action and write some text on the page at the end of the footer, just as a starting point, then we can work on storing that variable and adding a menu option to change that variable.

Add the following lines beneath the information header, and save your test.php file again. The first line uses the WordPress hook add_action, and the action used is wp_footer, which is triggered just before the </body> section of the template. It’s calling the function “my_function”, which you created just below the action.

 

Once you save the file browse to your site’s URL and you should see something at the very bottom of your page:

2

Congratulations! Your first plugin that does something to WordPress using an action is a great place to start.

Next, we want to create a menu option and change this over to a variable rather than just “hello world”. It will let you write whatever you want to the footer of your template, and change it via a textbox.

Add the following code to your test.php file, and once again, save it.

 

In a nutshell, this is calling another WordPress action called “admin_menu”, and running the function “my_admin_menu”. Inside the function “my_admin_menu” we are adding a management page, and calling another function when you visit that management page. The breakdown of the add_management_page function is below, and more information about it can be found here: https://codex.wordpress.org/Function_Reference/add_management_page

 

Once you refresh your admin dashboard after saving the file, you’ll notice that in the Tools menu there is an option called “Footer Text”.

3

 

When you go to that page, it will call the function we just created, and you’ll end up with the following:

4

Great! We’re more than halfway there. Let’s add a small form to it and change the function “my_function” to a variable instead of a fixed string. The function my_function will look like this now:

 

All this does is output the stored option ‘test_plugin_variable’ if it’s available (if it has been changed and saved), and ‘hello world’ if it’s not.

The footer_text_admin function will change to the following, which will create a simple form to update the variable, and a hook to update the variable if the form is submitted:

 

 

Once you save everything, you’ll see this on your admin dashboard:

5

As soon as you’re done changing it, it will automatically update the text that is output on the site too.

6

There you have it! A few quick and easy methods for accessing and storing variables in WordPress, adding a menu option, and using WordPress actions to perform your user defined functions. In the next post we will connect your plugin to MainWP and show you how to move everything over so that it lets you manage everything from within MainWP itself. Thanks for reading!

Leave a Reply

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