WordPress is a flexible platform which provides developer to add extra functionality without changing the core of WordPress. To enhance the functionality of WordPress we need to write custom plugins. WordPress Plugins are just like add-ons, which uses the core functionality of WordPress and add custom functionality into it.
In this article, we will look at what are WordPress plugins and how we can create one.
What is a Plugin in WordPress?
A plugin is a piece of custom code that extends the functionality of WordPress which already exists. It could be a single line of code or a bunch of lines.
Why use a WordPress plugin?
Whenever you get a burger from a fast-food restaurant, you will be asked ” Sir, do you want extra cheese in your burger? ”
Why extra cheese in a burger? Of course, it will enhance the taste of your burger but it is not necessary. You can have your burger without adding cheese to it.
WordPress is your burger and cheese is a plugin that will add new functionality or enhance the basic functionality of your WordPress.
Plugin Behavior
There are two types of basic plugin behaviors
add_action() and add_filter() both are WordPress hooks, to know more about hooks there is a complete tutorial for you on Some useful hooks in WordPress.
You want to perform some specific action using “add_action()” when:
-
A post is made
-
A post of a specific category is made
-
If a specific word occurs in the title of a post
-
If a new user added to a particular group of users, like admins group, editors group
And the list goes on depending on your requirements.
Let’s come to the presentation side. You want to perform some action using “add_filter()” when:
-
A post is presented(rendered) on the page
-
A post of a specific category is presented(rendered) on the page
-
If a specific word occurs in the title of a post
Practical example for “add_action()”.
We will make a plugin that will fetch a list of email addresses of the subscribers from the database and send a notification email to each user having a link to that newly published post whenever a new post will be published
Open a text editor and put this code snippet in the file and save it with any name in the ‘wordpress\wp-content\plugins’ directory.
<?php add_action(‘publish_post’, ‘notify’); function notify(){ echo ‘Notified'; } ?>
Job has been done. You have successfully created your new plugin. Go to the admin dashboard move the mouse over the plugins option and click on ‘installed plugin’ and see your plugin is coming in the list. What? It is not coming. Ok, no problem. Let’s tell WordPress about our plugin so that WordPress can know about it.
Open your plugin file and put these commented lines at the start of the file
/* Plugin Name: My Notify Plugin Description: With this plugin, you can notify a group of users about the publishing of a new post Author: WpBrigade Email: info@wpbrigade.com Version: 1.0 */
So now your plugin file should look like this.
<?php /* Plugin Name: My Notify Plugin Description: With this plugin, you can notify a group of users about the publishing of a new post Author: WpBrigade Email: info@wpbrigade.com Version: 1.0 */ add_action(‘publish_post’, ‘notify’); function notify(){ echo 'Notified'; } ?>
“publish_post” is a WordPress built-in function(hook) which is called when a post is to be published and what we are doing, we are telling the WordPress that when you call the “publish_post” function call our function also inside you “publish_post” function. This is called a hook. Like you don’t go and touch the core function and enhance that function in your own way.
Example for “add_filter()”.
Open your text editor again and put this code snippet into it.
<?php /* Plugin Name: Change Post Title Description: With this plugin, you can change the titles of the post Author: WpBrigade Email: infor@wpbrigade.com Version: 1.0 */ add_filter(‘the_title’, ‘add_text_to_post_title’); function add_text_to_post_title($title){ return $title. ‘ “Added when rendered”‘; } ?>
“the_title” is also a WordPress hook and we are doing the same thing with it what we have done with the “publish_post” hook.
Again keeping the file and folder name the same, and put the plugin folder into the plugins directory.
So this was a little about how to enhance WordPress functionality and plugins don’t stop here you can create plugins to add new functionality to WordPress.