Yogesh Chauhan's Blog

WordPress Plugin: Things to know

in WordPress on June 3, 2021

What’s a WordPress plugin?

Basically, it’s just a PHP file that includes a WP plugin header comment. You can get as complex as you want and make the website functionally very powerful with a plugin.

If you go to your wp-contents/plugins directory, you can see each plugin has its own folder which is actually recommended by WordPress developers for organizing all of your plugin files.

Steps to create a plugin

  1. Create a plugin directory inside wp-content/plugins/your_plugin_name.
  2. Add the plugin PHP file inside the plugin folder.
  3. Add a plugin header comment inside that PHP file.

What’s a plugin header comment?

If you have created a theme in WordPress, you know that it requires a style.css with theme meta data. Similarly, a plugin header comment is just PHP code block (in comments) that contains metadata. Metadata has the plugin info such as plugin name, plugin version, plugin author, licence (if any) and more.

Do you need to add all those metadata?

No. You can just have the name of the plugin and that should work just fine for your personal site use. But, if you’re developing a plugin for a public use then you would want all those metadata in plugin PHP file.

Do you need to have metadata in all PHP files of the plugin?

No. Just one of those PHP files have the metadata info if you have multiple PHP files.

A plugin header comment code example

This is how a typical plugin header comment code block looks like:

Plugin Name: YC Plugin
Plugin URI: https://yogeshchauhan.com/
Description: Made for this post: https://yogeshchauhan.com/wordpress-plugin-things-to-know/
Version: 1.0
Author: Yogesh Chauhan
Author URI: https://yogeshchauhan.com/
License: GPLv2+
Text Domain: yc-plugin

As soon as you save the PHP file, you can see the plugin in the plugins list in WordPress admin area.

Licence meta is required if you decide to share your plugin with others whether as a free or paid plugin. That helps other people understand how to use the plugin code. WordPress recommends that you pick a license that works with GNU General Public License (GPLv2+) to maintain compatibility with WordPress core.

What are WordPress Hooks?

WordPress hooks are a great way to change the WordPress behaviour without changing the core files of WordPress.

Type of WordPress hooks

There are two types:

  1. actions
  2. filters

With actions you can add or change functionality of WordPress website and with filters you can alter (filter) the content while it renders on the frontend side.

While some hooks are just kept for the developers — meaning they are just placeholders until you use them, many hooks are heavily used in the core WordPress development.

It’s basically like creating a furniture set with tons of availability and flexibility!

What hooks you need for a plugin development?

3 basic hooks you need to create a plugin:

  1. register_activation_hook() – runs when you activate the plugin
  2. register_deactivation_hook() – runs when you deactivate a plugin
  3. the register_uninstall_hook() – runs when you uninstall a plugin

With the register_activation_hook, you can add a function to setup the plugin. For e.g. you add a function to create some default settings in the options table. When user updates those settings, the table will get updated.

register_deactivation_hook clears temporary data stored by the plugin.

Finally, the register_uninstall_hook cleans up after you uninstall the plugin. This would delete all data created by the plugin as well as the data entered into options table. You can add an option to preserve the data as well.

How to add hooks?

You can use do_action() to add custom hooks.

You can pass all sorts of functions through the custom hooks. WordPress gives lots of flexibility to extend the plugin created by you. That’s how there are some great plugins available that are even greater the theme itself!

How to remove hooks?

You can use invoke remove_action() to remove the hooks you added.

For e.g. if you use a function callback to add a plugin using add_action() then you can use remove_action() with the same function callback. Note that remove_action() would need to run after the add_action().

Make sure you test it out when you add/remove a hook or even just alter the hooks priorities.

You should be careful when removing an action from a hook, as well as when altering priorities, because it can be difficult to see how these changes will affect other interactions with the same hook. We highly recommend testing frequently.

Use Options API from WordPress

WordPress has tons of APIs made for internal use and for developers as well. They are really helpful when you want to write a plugin. So, rather than coding everything in PHP and MySql to insert data in the database, you could just use one of the APIs to do so. The APIs functionality is not just limited to a database entry.

You can use Options API to store data in the database for the plugin you’re developing. You can use HTTP API if you want to use cURL in the plugin.

How WordPress Loads Plugins?

WordPress searches the plugins folder and subfolders inside wp-content directory and looks for the PHP files with plugin header comments.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to create a simple digital clock using JavaScript?JavaScriptCrypto.getRandomValues() method in JavaScriptJavaScriptNumber Properties in JavaScriptJavaScriptHow to create bouncing balls using HTML canvas and JavaScript?HTMLHow to create Flickering Texts using CSS?CSSWHERE Clause in PostgresPostgres