How To Create Your First WordPress Plugin

Creating WordPress plugins is a great way to add new functionality to your blog, site, or even into your eCommerce store. The biggest problem most users face when creating a plugin is that people really don’t understand how to create their first WordPress plugin and how easy it actually is. In this how-to post, we will teach you how to create your first WordPress plugin.

What is a plugin?

In short plugins are packages of code that help you extend the core functionality of WordPress core. To create a plugin you need to know PHP, CSS, JS, and HTML.

Difference between plugins and functions.php

Now if you’re familiar with WordPress functions.php you already know how to add functions and code to your WordPress site. The main difference between a plugin and the functions.php file is that a plugin can be simply deactivated through the Plugins tab in your WordPress settings whereas a function in functions.php needs to be completely removed or commented out of code to stop it from being run.

Here’s what WordPress says in their Introduction to Plugin Development:

If there’s one cardinal rule in WordPress development, it’s this: Don’t touch WordPress core. This means that you don’t edit core WordPress files to add functionality to your site. This is because WordPress overwrites core files with each update. Any functionality you want to add or modify should be done using plugins.

WordPress.org

How to create a WordPress plugin

To create a plugin and to make it do something we will need to go through these steps:

  1. Create a plugin folder
  2. Set up plugin header
  3. Create plugin functionality
  4. Load plugin to WordPress
  5. Activate plugin

1. Create plugin folder

Depending on where you are developing your plugin you either need to log in to your server through FTP, CPanel, or another system that allows you to create the folder.

The folder needs to be created in WordPress > wp-content > plugins > PLUGIN-NAME

Remember to make the folder name short and similar to your plugin’s name.

2. Set up plugin header

To get started you need to create your basic plugin header or in common terms a PHP file that contains certain information. Information you can add to the header includes the following:

  • Plugin Name: (required) The name of your plugin, which will be displayed in the Plugins list in the WordPress Admin.
  • Plugin URI: The home page of the plugin, which should be a unique URL, preferably on your own website. This must be unique to your plugin. You cannot use a WordPress.org URL here.
  • Description: A short description of the plugin, as displayed in the Plugins section in the WordPress Admin. Keep this description to fewer than 140 characters.
  • Version: The current version number of the plugin, such as 1.0 or 1.0.3.
  • Requires at least: The lowest WordPress version that the plugin will work on.
  • Requires PHP: The minimum required PHP version.
  • Author: The name of the plugin author. Multiple authors may be listed using commas.
  • Author URI: The author’s website or profile on another website, such as WordPress.org.
  • License: The short name (slug) of the plugin’s license (e.g. GPLv2). More information about licensing can be found in the WordPress.org guidelines.
  • License URI: A link to the full text of the license (e.g. https://www.gnu.org/licenses/gpl-2.0.html).
  • Text Domain: The gettext text domain of the plugin. More information can be found in the Text Domain section of the How to Internationalize your Plugin page.
  • Domain Path: The domain path lets WordPress know where to find the translations. More information can be found in the Domain Path section of the How to Internationalize your Plugin page.
  • Network: Whether the plugin can only be activated network-wide. Can only be set to true, and should be left out when not needed.
  • Update URI: Allows third-party plugins to avoid accidentally being overwritten with an update of a plugin of a similar name from the WordPress.org Plugin Directory. For more info read related dev note.

If we want to turn this into an acceptable header you can use our example or even create your own and insert it into your PHP file named my-example-plugin.php

/**
 * Plugin Name:       My Example Plugin
 * Plugin URI:        https://bloginbox.com
 * Description:       Add "Thanks for reading!" text under each post
 * Version:           1.0.0
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            Blog Inbox
 * Author URI:        https://bloginbox.com
 * License:           GPL v3 or later
 * License URI:       https://www.gnu.org/licenses/gpl-3.0.html

 * Text Domain:       my-example-plugin
 * Domain Path:       /languages
 */

3. Create plugin functionality

Now that we have created the header, let us create the functionality. In this example, we will create a plugin that will simply add code below each of your blog posts saying “Thanks for reading!”, though you can change this and even add some other functionality if you wish to do so.

add_action( 'the_content', 'my_text_function' );

function my_text_function ( $content ) {
    return $content .= '<p>Thanks for reading!</p>';
}

The “add_action” creates an action that will be only fired on content pages. To do this it hooks into the “the_content” hook that tells it when to fire our function. You can read more about hooks and filters here.

4. Load plugin to WordPress

Now that we are done creating the plugin all we need to do is turn the folder into a zip file and go load it to our WordPress page in Plugins > Add new >Upload Plugin.

5. Activate your plugin

To activate your plugin simply head on to “Plugins” in your WordPress admin and click “Activate”

Conclusion

In conclusion, creating your own plugin is exciting and it’s a great way to extend your WordPress core functionality. For more information on how to build your own WordPress plugin we highly recommend following WordPress’s official plugin guide, so you can ensure compatibility and that your plugin code follows WordPress basic guidelines.

Feel free to copy-paste the full plugin code from below to get started with your own WordPress plugin development.



/**
 * Plugin Name:       My Example Plugin
 * Plugin URI:        https://bloginbox.com
 * Description:       Add text under each post
 * Version:           1.0.0
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            Blog Inbox
 * Author URI:        https://bloginbox.com
 * License:           GPL v3 or later
 * License URI:       https://www.gnu.org/licenses/gpl-3.0.html

 * Text Domain:       my-example-plugin
 * Domain Path:       /languages
 */

add_action( 'the_content', 'my_text_function' );

function my_text_function ( $content ) {
    return $content .= '<p>Thanks for reading!</p>';
}

Don’t forget to share and comment if you liked this tutorial.

Similar Posts

Leave a Reply

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