Create your first Plugin in 3 minutes

In this tutorial we are going to make a very simple basic WordPress plugin. To do so we have to go to our WP installation folder, to the folder “wp-content”. Underneath to the folder “plugins” and within this folder we are going to make our plugin folder, which in this case is called “some-basic-plugin”. Underneath we make a PHP file which is called the same “some-basic-plugin.php”.

Create plugin folder and file

When we open de PHP file, we start with the opening PHP tag. We make some comments, which is called the file header. The file header contains some information which WP needs to know that our plugin exists. In this case we added the plugin name, version, description and author.

/*
Plugin Name:    Some Basic Plugin
Version:        0.1.0
Description:    Demo Plugin Development Tutorial
Author:         Victor Villaverde Laan
*/

This is a very basic file header. If we take a look at the admin part of WordPress, we can see that the plugin is already added to the list. And as you can see the name is “Some Basic Plugin”. It has a description, the version and the author name, which is exactly the information we have put in the file header.

Add admin notice

So now we already created a plugin. But the plugin doesn’t do anything. So now we are going to add some functionality. In this case we are only going to add an admin notice when the plugin is activated. To do so we need to use the built-in action of WP. So we are going to use “add_action” and the action we want to use is “admin_notices”. And we add a function to it. In this case we call the function “some_basic_plugin_show_activated”, separated by underscores, because that’s the naming convention of WordPress.

add_action('admin_notices', 'some_basic_plugin_show_activated');

function some_basic_plugin_show_activated()
{
}

It’s a very long function name, but it’s a good practice to use prefixes for your functions, at least for your global functions. In this case we used the prefix “some_basic_plugin_”. We use this prefix to prevent conflicts with other functions. So in this case we know our name is unique, because we used our own prefix.

Now we have to create the function itself. It’s a global function with the same name “some_basic_plugin_show_activated”. This function has to show the notice we want to add. So we added some HTML and then we added our notice description.

add_action('admin_notices', 'some_basic_plugin_show_activated');

function some_basic_plugin_show_activated()
{
?>
    <div class="notice notice-success">
        <p>Yeah, Some Basic Plugin is activated!</p>
    </div>
<?php
}

Test it

Now let’s have a look if it works. When we go to the admin part and click on the “activate” link of our plugin, then you can see that our notice is added on top of the page. In this case the notice will be shown on every admin page as long as the plugin is activated. So when we deactivate our plugin the notice is gone. And that’s our basic plugin.

<?php
/*
Plugin Name:    Some Basic Plugin
Version:        0.1.0
Description:    Demo Plugin Development Tutorial
Author:         Victor Villaverde Laan
*/

add_action('admin_notices', 'some_basic_plugin_show_activated');

function some_basic_plugin_show_activated()
{
?>
    <div class="notice notice-success">
        <p>Yeah, Some Basic Plugin is activated!</p>
    </div>
<?php
}

More File Header information

You can also add some additional information to the plugin file header. It could look like this, also containing a plugin URL, an URL of the author, license, text domain (used for localization of the plugin), the domain path (the directory of the translation files) and network (used for plugins that need only to be activated on WP multi sites).

/*
Plugin Name:    Some Basic Plugin
Plugin URI:     http://www.freelancephp.net
Version:        0.1.0
Description:    Demo Plugin Development Tutorial
Author:         Victor Villaverde Laan
Author URI:     http://www.freelancephp.net
License:        Dual licensed under the MIT and GPL licenses
Text Domain:    some-basic-plugin
Domain Path:    /languages
Network:        true
*/

PHP DocBlocks

For those who use PHP docblocks, WP can also fetch the plugin information from DocBlocks like this.

/**
 * Some Basic Plugin
 *
 * @package Some Basic Plugin
 * @author  Victor Villaverde Laan
 *
 * @wordpress-plugin
 * Plugin Name:    Some Basic Plugin
 * Version:        0.1.0
 * Description:    Demo Plugin Development Tutorial
 * Author:         Victor Villaverde Laan
 */

Well this was the short demo on how to create a plugin in WordPress.

5 Comments

  • Jeff Klein 29 October 2016 Reply

    Thanks for the article. I have thought about creating my own plugin but wasn’t sure how to do it. Based on the info you provided. I will give it a try.

    • victor 29 October 2016 Reply

      Good luck and enjoy building your first plugin!

  • Kiandra Plummer 20 December 2016 Reply

    HI Victor,

    This was easy to follow tutorial, short and to the point..loved it!

    FYI, I did have to rearrange some of the code. The written code in the article is different than what is in the video .

    add_action('admin_notices', 'portfolio_basic_plugin_show_activated');
    
    function portfolio_basic_plugin_show_activated()
    {
    ?>
            Yeah, Some Basic Plugin is activated!
    <?php
    }
    
    • victor 21 December 2016 Reply

      Thanks Kiandra.
      I updated the code.

  • martin 5 July 2017 Reply

    howdy – can you do some more of this great tutorials – well that just be great

    Love to hear from you

    martin

Leave a Reply