creating-wordpress-plugins-tutorial

Creating A WordPress Plugin Is Easier Than You Think

Until a few years ago, I hadn’t written a single WordPress plugin. I had created and customized many themes for our clients, but for some reason, I kept telling myself that creating a plugin was beyond my capabilities.

In hindsight, I couldn’t have been more wrong.

If you’ve ever felt this way, let me tell you something. Creating a WordPress plugin is not beyond your capabilities. Anyone that has skills enough to write basic PHP and modify a theme can create a plugin.

This is how I started the Beaver Builder plugin (it’s free so you can try it) and how you can start yours too.

We just launched 6 FREE Beaver Builder courses. Learn how to easily build WordPress websites with step-by-step video tutorials. Get started today.

Why would you want to create a plugin?

If you’re like I was, you’ve probably been adding functionality to your theme instead of creating a plugin. There are plenty of cases where doing so is fine, but there are also cases where custom functionality is better off being added to a plugin. Why might you ask?

Consider this scenario.

You’ve added functionality to your theme that changes the default gravatar to your own custom gravatar. The only issue is, you’ve just changed themes and now that’s gone. If you had added that code to a plugin it would still be there when you decided to switch themes.

We ran into this issue with the Tabata Times multisite network. They use a handful of themes that need to share custom functionality. How do you think we solved that problem? You guessed it, by adding a good chunk of the functionality into a plugin so it is available to all sites on the network, regardless of which theme they are using.

Don't lock yourself into a theme. Use #WordPress Plugins for functionality instead. Click To Tweet

Create your first plugin in five simple steps

I’m not kidding. You can create a WordPress plugin in five simple steps. Let me show you how…

1. FTP into your site

The first thing you’ll need to do is access your site via FTP using the FTP program of your choice (mine is Coda). If you’re not familiar with FTP, I recommend you read up on that before moving forward.

2. Navigate to the WordPress plugins folder

Once you’ve accessed your site via FTP, you’ll need to navigate to the WordPress plugins folder. That folder is almost always located at /wp-content/plugins.

3. Create a new folder for your plugin

Now that you’re in the plugins folder it’s time to create a folder for yours! Go ahead and create a new folder, giving it a unique name using lowercase letters and dashes such as my-first-plugin. Once you’ve done that, enter your new folder and move on to the next step.

4. Create the main PHP file for your plugin

Next, you’ll need to create the main file for your plugin. To do so, create a PHP file within your new plugin folder and give it the same name such as my-first-plugin.php. After you’ve done that, open your plugin’s main file and get ready to do some editing.

5. Setup your plugin’s information

Finally, copy and paste the plugin information below into your main plugin file. Make sure to edit the details such as Plugin Name and Plugin URI as they pertain to your plugin.

<?php
/**
 * Plugin Name: My First Plugin
 * Plugin URI: http://www.mywebsite.com/my-first-plugin
 * Description: The very first plugin that I have ever created.
 * Version: 1.0
 * Author: Your Name
 * Author URI: http://www.mywebsite.com
 */

That’s it! You’ve just completed the minimum number of steps that are required to create a WordPress plugin. You can now activate it within the WordPress admin and revel in all of your glory.

What now?

At this point, you’re probably wondering what this plugin is supposed to do. Well, it doesn’t do anything! I said I would show you how to create a plugin, I didn’t say I’d show you how to create a plugin that does anything. 🙂

All kidding aside, the goal of this post is to illustrate just how simple it is to get started creating WordPress plugins. Whip one up with the steps outlined above and you’re ready to start making things happen.

Making your plugin do something simple

Now that you have a plugin, let’s make it do something.

The easiest way to make things happen in WordPress is with actions and filters. Let’s explore that by creating a simple action that adds a line of text below all of the posts on your site. Copy and paste this code into your main plugin file (below the plugin information) and save it.

add_action( 'the_content', 'my_thank_you_text' );

function my_thank_you_text ( $content ) {
    return $content .= '<p>Thank you for reading!</p>';
}

This code hooks into “the_content” action that fires when WordPress renders the post content for your site. When that action fires, WordPress will call our “my_thank_you_text” function that is defined below the “add_action” call.

Going beyond a simple plugin

cockpit

If you’ve made it this far, hopefully, we’re in agreement that creating a simple WordPress plugin is relatively easy. But what if you want to create a plugin that does more than accomplish one simple task?

Actions and Filters

If you’re going to start coding your own plugins, I highly suggest you familiarize yourself with how actions and filters work and which ones are available for you to use. The WordPress Codex is where I spend a lot of my time, I suggest you do the same.

Plugin API: Actions and Filters
Plugin API: Action Reference
Plugin API: Filter Reference

WordPress Functions

Again, I spend a lot of my time in the WordPress Codex reading up on core functions as I develop my plugins. There are so many core functions that I wouldn’t expect you to know what each and every one of them is and does. That’s what the Codex is for, after all, so use it!

 

Creating an Options Page

Finally, if you end up creating a plugin that does something nifty, you’ll probably want to create an options page so people that use it can modify the functionality. Creating an options page isn’t necessary, there are many plugins that install and do something without one, but having one can be a nice addition for users of your plugin.

Creating an options page is beyond the scope of this post, so once again, I’ll leave you in the hands of the WordPress Codex.

Writing a Plugin
Creating Options Pages

If you haven’t already, create your first plugin!

Creating WordPress plugins is extremely liberating and a great way to gain a deeper knowledge of how WordPress works. If you haven’t already, I strongly urge you to try your hand at creating a plugin. If you do and come up with sometimes useful, don’t forget that you can distribute it freely to others via the WordPress plugin directory.

Have you already created your first plugin or plan on creating one soon? If so, I would love to hear about it in the comments below!

It would be totally gnaw-some if you could share this post with your friends.
Justin Busa's Bio

38 Comments

  1. ganesh on April 14, 2018 at 9:59 am

    great and inspiring too



  2. Ercan on August 19, 2018 at 5:48 pm

    An inspiring and firing article.
    Especially for me like you mentioned yourself as being afraid of the code side of WordPress.

    I like and use WordPress a lot.
    When it comes to add some fetaures to it I go for plugins

    But as you mentioned, the plugins have some missing functions to fulfill all my needs.

    So I get blocked to go further in my projects. Because I can’t do the necessary modifications to the codes.

    Thus I really look forward to beIng able to cope with the codex of WordPress and get my way out through the projects.

    Thanks again for the inspiring and motivating content.



  3. Ebuka on September 1, 2018 at 4:28 pm

    Nice. Good enough for a jumpstart



  4. Charles on November 7, 2018 at 9:56 pm

    What a nice article! How about another about converting a html template to a wordpress theme?



    • Robby McCullough on November 8, 2018 at 10:13 am

      Good idea! That might be a good one to do after Gutenberg is live…



  5. Kendra Joyner on November 13, 2018 at 12:51 pm

    This was incredibly helpful! Thanks



  6. Chuks on January 10, 2019 at 7:17 pm

    Very useful icebreaker for people like me that had the same phobia of looking ‘únder the hood’ of plugins! Thanks.



  7. Faithful on March 9, 2019 at 3:25 pm

    Woow! Thanks so much for this, I can write plugins now. always wondered how those guys do write them, never knew it was this easy! My doubts have been cleared.



  8. Casey on March 17, 2019 at 2:35 pm

    I always thought child themes were the best way to solve this problem, but alas, if I change parent themes, I lose the functionality. This makes a lot of sense. Thank you!



    • Robby McCullough on March 17, 2019 at 8:35 pm

      Glad you found the article helpful!



  9. Theo on March 19, 2019 at 2:19 pm

    Yep! You got me at “I didn’t say I’d show you how to create a plugin that does anything. 🙂”
    Great way to teach and keep the user engaged!

    I am now adding admin interface to my plugin… thanks so much!



  10. Maximillian Laumeister on March 25, 2019 at 12:22 pm

    I think you may be missing a PHP closing tag: ?> Thanks for the tutorial!



    • Ricardo Andrade on April 2, 2019 at 9:51 am

      If a file contains only PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.



      • Maximillian Laumeister on April 2, 2019 at 7:52 pm

        Interesting. The other tutorials I read include the closing tag so I just assumed it was a mistake, but you’re totally right.



  11. Ali on April 15, 2019 at 12:47 am

    Thank you



  12. jadenewport on April 16, 2019 at 11:23 pm

    Thank you for this post for all of us WP rookies 🙂 May I ask a silly question – if I add the PHP code from my child theme’s functions.php into the new plugin’s main php file, it will do the same thing but it will be independent from the theme, right? Thank you 😊



    • Robby McCullough on April 24, 2019 at 1:17 pm

      It should do the same thing, yes!



      • Bongquotes on January 14, 2020 at 10:27 am

        Thank you so much for sharing this. Today I manage 3 plugins of my own. It all started with this post. Thanks, Rob.



    • Gktoday on May 30, 2021 at 12:06 pm

      After reading your whole article very carefully then I create my first WordPress plugin. Thanks man for sharing your valuable information.



  13. Deepak Bharti on May 14, 2019 at 5:42 am

    Nice post it is helpful tips for me. Thanks for sharing.



  14. Njofie Wilson on June 20, 2019 at 2:01 am

    Thanks for this great post that has boosted my confidence in touching WordPress code and creating my own plugins.



  15. Manolis on July 27, 2019 at 1:20 am

    Hi and thanks for the info

    Is it add_action or add_filter in the above code?



  16. sunny on August 13, 2019 at 10:01 pm

    as I know the ‘the_content’ is a filter not an action. So instead of add_action you should use add_filter



  17. Abhineet Mittal on August 18, 2019 at 11:00 pm

    Great post. But there is a little bug in the code. Instead of add_action, you must use add_filter as the_content is a filter.



  18. Raphael Akpofure on September 27, 2019 at 12:46 am

    Thanks alot! very helpful



  19. Kamar on November 28, 2019 at 11:02 am

    Hi,
    I have realized your tutorial and have had some ideas regarding making a plugin. I seem it is really a nice article for every guy that would be prolific for the beginners. More might be gained and hope you will help me.



  20. Aimal Khan on December 29, 2019 at 3:12 am

    Excellent! A very easy and handy approach to developing WP plugins, I liked it.

    I will be creating a calculator plugin for my WP, which I wasn’t able to find it anywhere online.

    Thanks for sharing



  21. pangip on January 4, 2020 at 8:48 pm

    Can i use this steps for WordPress 5.3.x ?



  22. Stephen on January 18, 2020 at 2:10 pm

    Hi, thanks for the basics, as most have said but having looked around Wordpress, as suggested, I can’t find the “How to add” a “View Details” page. You know like, how many people are using it, star ratings, etc. A link or post on this subject would be good.
    Thanks



  23. Vadoh on January 24, 2020 at 9:03 pm

    This is wow! I thought creating a plugin will cost me heaven and earth.
    Thanks for the great post 🙂



  24. JB on January 30, 2020 at 4:07 pm

    Thanks exactly what I needed!



  25. Dan Martin on March 1, 2020 at 6:27 pm

    This is great, as someone trying to decide if I want to jump into plugin development this was really helpful.



  26. Luca Spinelli on May 1, 2020 at 6:26 am

    Today i built my 1st plugin. Thanks for the inspiration 🙂



  27. Matthew Granat on May 17, 2020 at 9:09 am

    This was awesome article! Just made a quick plugin using your BB module usage filter. Woohoo!



  28. Heather New on June 22, 2020 at 3:23 pm

    I haven’t created a plugin, yet however I do regularly create custom content for themes. When is it good to use a plugin and when would you advise against it?

    I’m considering what pieces of my current theme I want to break out into plugins to help with version control.

    I don’t want to have a ton of plugins activated in the site but also looking to implement plugins when it is smart to do so and I’m interested in your thoughts about this.



  29. Sayeed on August 26, 2020 at 11:10 pm

    Nice one.
    I was thinking creating wordpress plugin is very difficult task but after reading this blog my mind is totally changes
    thank you.



  30. Forkan on March 24, 2021 at 2:52 am

    Perfect Post! I really like your simple and perfect message. Thanks a lot for your nice share. I will lots of time in Codex for learning your tips. Thanks again



  31. Anutosh Roy on June 17, 2021 at 8:11 pm

    Just amazing Post. It’s helpful for all blogger who are using WordPress.



Our Newsletter

Our newsletter is personally written and sent out about once a month. It's not the least bit annoying or spammy.
We promise.

Try Beaver Builder Today

It would be totally gnaw-some if you could share this post with your friends.

Related articles

How to Restrict Content in Beaver Builder

How To Add Content Restriction in Beaver Builder

Wondering how to restrict content in Beaver Builder? If you run an online business built around premium content or educational…

Read More...
Best WordPress Black Friday deals

Best WordPress Black Friday Deals (2023)

Hey Beaver Builders! Are you looking for the best WordPress deals for this holiday season and Black Friday? We reached…

Read More...
How to Redirect a WordPress Page

How to Redirect a WordPress Page (Manually and Using a Plugin)

In this article, we’ll dig into what redirects are and why you might need or want to use them. We'll...

Read More...

Join the community

We're here for you

There's a thriving community of builders and we'd love for you to join us. Come by and show off a project, network, or ask a question.

Since 2014

Build Your Website in Minutes, Not Months

Join Over 1 Million+ Websites Powered By Beaver Builder.