WordPress Plugin Tutorial: How to Create a WordPress Plugin

In this WordPress plugin tutorial you will learn how to create a complete WordPress plugin with its own admin page. The most important reason to create a plugin is that it allows you to separate your plugin code from the WordPress code. If something goes wrong with your plugin, the rest of the site can usually continue to function without issue.

Create a WordPress plugin in 4 easy steps:

  1. Plugin storage
  2. Create the first file
  3. Write the plugin functions
  4. Create the new admin page

Altering the core WP code can be catastrophic. But, with the advanced features that WordPress gives you, you can create plugins in a way that changes the way basic functions are handled without altering your code.

What do you need?

To complete the steps in this WordPress plugin tutorial, you will need a text editor such as or You will also need FTP access to your hosting account and WordPress to be running on your site.

You can learn how to connect Notepad++ to your FTP server by reading our tutorial on . You can also use FTP software like FileZilla to upload your files, and our tutorial on will help you with this.

This WordPress plugin tutorial is intended for people who already have basic knowledge of PHP. You will write a new function, call existing WordPress functions using parameters, and write PHP comments.

We also recommend, above all else, to create a backup of your website before proceeding.

What are WordPress plugins?

A wordpress-plugins it is an independent set of lines of code that enhance and extend the functionality of WordPress. By using any combination of PHP, HTML, CSS, JavaScript/jQuery, or any other web programming language, a plugin can give new features to any part of your website, including the Admin Control Panel. You can modify the default behavior of WordPress or completely remove unwanted behavior. Plugins allow you to easily modify and customize WordPress to suit your needs.

Because WordPress plugins are standalone, they do not physically alter any core WordPress code. They can be copied and installed on any site that has WordPress installed. An alternative way (and not recommended) of making changes to WordPress is writing new functions to the file functions.php of WordPress, which is stored in the folder /wp-includes/or in the file functions.php which is part of your theme. However, this could trigger a number of potential problems.

WordPress and its themes receive regular updates. And, unless you’re using the , when the file functions.php is overwritten by an update, your code will be deleted and you will have to write it again. If you write a lot of functions and one of them has a bug that you can’t debug, you’ll have to replace the entire file with the original, removing all the changes you made. If your functions are removed from the file, your website may experience certain PHP errors when trying to call the missing functions.

See also  What is Node.js: Common use cases and how to install it

Plugins are never automatically overwritten or removed when installing WordPress updates. If your plugin has coding errors, you can usually only disable it in the Admin Control Panel while you’re making changes. If your plugin has a fatal error, WordPress will automatically disable it, thus allowing your site to continue working.

What are hooks?

WordPress plugins interact with core code using hooks, which in Spanish would be translated as hooks. There are two different types of hooks.

  1. Action hooks (to add/remove functions)
  2. Filter hooks (to modify data returned by functions)

Actions and Action Hooks

When visiting any page of a WordPress website, a series of PHP functions (called Actions) are called at various points, and are associated with the action hook. Using the action hooks provided by WordPress, you can add your own functions to the list of actions that are executed when any action hook is called, and you can also remove pre-existing functions associated with any action hooks. Action hooks dictate when actions are called. before the label closing of any page, the action hook is called wp_head() and the actions associated with it are executed. wp_head().

Action hooks are contextual: some are called on every page of your website, others are only called when the Admin Dashboard is displayed, etc. A full list of action hooks and the context in which they are called can be found on the .

Adding functions to an action hook using add_action()

For add a function to any action hook, your plugin must call the WordPress add_action() function with at least two parameters.

// Hook to the ‘init’ action, which is called after WordPress is finished loading the core code add_action( ‘init’, ‘add_Cookie’ ); // Set a cookie with the current time of day function add_Cookie() { setcookie(“last_visit_time”, date(“r”), time()+60*60*24*30, “/”); }

  • The first required parameter is the name of the action hook that you want to associate.
  • The second required parameter is the name of the function that you want to run.
  • The third parameter (optional) is the priority of the function you want to execute. You can attach any number of different functions to the same action hook, and order them any way you like. The default priority is 10, putting your custom function after any of WordPress’ built-in functions. In that case, a function with priority 11 is executed next, and so on.
  • The fourth parameter (optional) is the number of arguments, which means how many parameters your custom function can take. The predetermined value is 1.
See also  Free Custom Domains For Everyone |

Example of plugin code to display text after the footer of each page

This plugin associates the action hook wp_footer()which is called just before the tag closing of each page, and adds a new function called mfp_Add_Text(). Since this is part of a plugin and not the theme, it will continue to work even if you activate a totally different theme. You can save this example as a PHP file, upload it to the folder wp-content/plugins/ and activate it in the Admin Control Panel to see the change.

After the footer is loaded, my text is added!

“; }

Final score:

NOTE: you can call add_action() before defining your own function, as PHP evaluates the entire script before executing it. Write the calls add_action() at the top of the file in the order they are executed, and then defining your functions in the same order, makes the file easier to read.

Removing functions from an action hook using remove_action()

For remove an action from an action hook, you must write a new function that calls remove_action() and then call the function you’ve written using add_action(). The following example will make it clearer.

You must give remove_action() at least two pieces of information such as parameters.

// Hook the ‘init’ action, which is called after WordPress is finished loading the core code, add the function ‘remove_My_Meta_Tags’ add_action( ‘init’, ‘remove_My_Meta_Tags’ ); // Remove the ‘add_My_Meta_Tags’ function from the wp_head action hook function remove_My_Meta_Tags() { remove_action( ‘wp_head’, ‘add_My_Meta_Tags’); }

  • The first required parameter is the name of the action hook to which the function is associated.
  • The second required parameter is the name of the function that you want to delete.
  • The third parameter (optional) is the priority of the original function. This parameter must be identical to the priority that was originally defined when adding the action to the action hook. If you didn’t define a priority in your custom function, don’t include the parameter.

Example: Imagine that you do not want the text added to the footer, with the example above, to appear on Mondays. One way to do this is to use the PHP date function to get the current day, followed by a conditional statement if… then… to test if the current day is Monday, and then call remove_action() Yes it’s monday. Call the function using add_action() attaching any action hooks that occur before the action hook you want to change. If you try to hook an action hook that occurs after wp_footerin this example, the action will not be removed from the hook since it will have already been executed.

See also  How to Fix ERR_TOO_MANY_REDIRECTS Error in WordPress

After the footer is loaded, my text is added!

“; } // Define the function named ‘mfp_Remove_Text()’ to remove our previous function from the ‘wp_footer’ action function mfp_Remove_Text() { if (date(“l”) === “Monday”) { // Target the ‘wp_footer ‘ action, remove the ‘mfp_Add_Text’ function from it remove_action(“wp_footer”, “mfp_Add_Text”); } }

Filters and filter hooks

A filter function allows you to modify the resulting data that is returned by existing functions and must be hooked to one of the filter hooks. The available filter hooks are different from action hooks, but they behave similarly to action hooks because of the way they are called at various points in the script and because they are contextual. A complete list of filter hooks and the context in which they are called can be found in the .

Adding filters using add_filter()

To add a filter function to any filter hook, your plugin must call the WordPress add_filter() function, with at least two parameters.

// Hook the ‘the_content’ filter hook (content of any post), run the function named ‘mfp_Fix_Text_Spacing’ add_filter(“the_content”, “mfp_Fix_Text_Spacing”); // Automatically correct double spaces from any post function mfp_Fix_Text_Spacing($the_Post) { $the_New_Post = str_replace(” “, ” “, $the_Post); return $the_New_Post; }

  • The first required parameter is the name of the filter hook that you want to associate.
  • The second required parameter is the name of the filter function that you want to run.
  • The third parameter (optional) is the priority of the function you want to execute. You can associate any number of different filter functions to a single filter hook. The…
Loading Facebook Comments ...
Loading Disqus Comments ...