Step by step guide to make your first WordPress Plugin

Plugin in WordPress is a component to add or extend functionality that can be enabled or disabled as required and makes no interference with the core program/software and its code. WordPress is designed to be lightweight and smooth to increase the flexibility and reduce code that might not be important for basic website functions. Plugins then add custom features and functionalities that allow users to design the site as their specific requirements.

In this guide, you will learn how to develop your first WordPress plugin.

Prerequisites:

Steps to make a plugin –

  1. Goto the WordPress Installation -> WordPress/wp-content/plugins.
  2. Make a new folder here and name it as the “Plugin Name” in this case First-Plugin
  3. Next, make a new PHP file in this folder and name it same as the folder in this case First-Plugin.php
  4. Now, we add the name of our plugin. Add Plugin Name enclosed in a PHP comment in the file.
    Code of PHP file First-Plugin.php –

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    /**
    * Plugin Name: First Plugin
    **/
    ?>

    chevron_right

    
    

  5. Now, Goto the WordPress Dashboard->Plugins, Here you can see our newly created plugin and its name.
  6. We should not leave the description blank as it helps the user know what is plugin meant to do. so let’s add the description of our plugin by adding a new comment line after “Plugin Name:” with the title “Description” and save it. Code of PHP file First-Plugin.php –
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    /**
    * Plugin Name: First Plugin
    * Description: This is my first Plugin.
    **/
    ?>

    chevron_right

    
    

  7. We Successfully added Description of our plugin now go to Dashboard->plugin to see how it looks like.
  8. Next, we add some functionality to our first Plugin. We will add a basic function named first_plugin() which prints “HELLO This is my first Plugin.” and its shortcode which allows this to be placed on a WordPress post or a Page.
    Code –

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    /**
    * Plugin Name: First Plugin
    * Description: This is my first Plugin.
    **/
      
      function First_Plugin()
      {
      
          $content="HELLO This is my first Plugin.";
          return $content;
      }
      add_shortcode('myplugin', 'First_Plugin')
     ?>

    chevron_right

    
    

  9. To display this function output value we are using the shortcode feature of WordPress. Go to Posts->edit or create a new post. There add the shortcode for the Plugin we just made.
    Shortcode – [myplugin]
  10. To see the plugin output, browse the website and open the post in which we added the shortcode of our plugin.
    Here is how it looks like-

  11. Now, To have this plugin functionality used multiple times we can use this shortcode multiple times. Just add the shortcode wherever we need this output.
  12. Check the post for updated output here we get two same sentences because we used the shortcode two times in this post.

Now we have our Plugin which returns a value and this can be used wherever we want by the use of Shortcode.



My Personal Notes arrow_drop_up

Final Year Student Programming Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.