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:
- Plugin storage
- Create the first file
- Write the plugin functions
- 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.
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.
- Action hooks (to add/remove functions)
- 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.
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