fbpx

Create a WordPress Plugin

Read Time:3 Minute, 20 Second

There are currently 59,990 free plugins in the WordPress plugins repository. Developing a new plugin is similar to writing code for WordPress in the theme. You still use WordPress actions and filters. This guide will explain how to easily create a WordPress plugin from scratch.

Code in Theme VS in Plugin

The main difference between coding in the theme’s files to developing in a separate plugin is that when adding code to the theme, the code becomes a part of it. This means if you want to then change the theme for any reason, the code will be “deactivated” since you are no longer using the theme you added your code to. On the other hand, when you add code to your website as a plugin, you will be able to deactivate only this portion of the code and it won’t be theme-dependent.

Create a WordPress plugin – Metadata

When you open the plugins management page in your WordPress website’s admin dashboard, you can see details about each plugin.

You can see details about each plugin

These details are the plugin’s metadata which is added to the start of the plugin’s main file.
Let’s start.

To create your plugin, create a new folder with your plugin’s name. Notice, the name should be lowercase and with dashes instead of spaces. For example, if your plugin is called “My Coolest WordPress Plugin”, the folder’s name will be “my-coolest-wordpress-plugin”. In this folder, create a new PHP file with the same name as the folder’s name. According to our example, the file’s name will be “my-coolest-wordpress-plugin.php”.

Open the file you just created and add the following code to the start of it.

<?php
/**
 * Plugin Name
 *
 * @package           MyCoolestWordpressPlugin
 * @author            KodaCoding
 * @copyright         2022 KodaCoding
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       My Coolest WordPress Plugin
 * Plugin URI:        https://kodacoding.com/create-a-wordpress-plugin
 * Description:       Create a WordPress plugin tutorial on KodaCoding.com
 * Version:           1.0.0
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            KodaCoding
 * Author URI:        https://kodacoding.com
 * Text Domain:       my-coolest-wordpress-plugin
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Update URI:        https://kodacoding.com/create-a-wordpress-plugin
 */

Save your file and upload your new plugin to your website. You can follow our guide on how to upload a new plugin to WordPress.

After you uploaded your plugin to your WordPress website, go to the plugins page. Once there, find your plugin and activate it.

Find your plugin and activate it

Now, your plugin is active but since we did not add anything other than the metadata to the plugin, it is still not doing anything. Now it is time to add some functionality to the plugin. You can do this by using WordPress’ actions and filters the same way you would use them in the theme.

If you’re not sure, let’s add a short text to the footer.
After the plugins metadata in the main plugin’s name, add the following code.

<?php
/**
 * Plugin Name
 *
 * @package           MyCoolestWordpressPlugin
 * @author            KodaCoding
 * @copyright         2022 KodaCoding
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       My Coolest WordPress Plugin
 * Plugin URI:        https://kodacoding.com/create-a-wordpress-plugin
 * Description:       Create a WordPress plugin tutorial on KodaCoding.com
 * Version:           1.0.0
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            KodaCoding
 * Author URI:        https://kodacoding.com
 * Text Domain:       my-coolest-wordpress-plugin
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Update URI:        https://kodacoding.com/create-a-wordpress-plugin
 */

// Add some text to the footer
function kodacoding_add_some_footer_text() {
    echo "This is some text from our awesome plugin!";
}
add_action( 'wp_footer', 'kodacoding_add_some_footer_text' );

That’s it, now you can keep adding actions and filters to your WordPress plugin.
You can also learn how to use actions and filters in classes in WordPress.

Leave a Reply

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