There’s no doubt that the web is full of great tutorials on getting started with real WordPress development. Lots of developers far more talented than myself have provided helpful guides on plugin development. Even so, I am going to take the time to contribute my own tips to those wishing to take WordPress to the next level.
The basics
I think its always constructive to begin with the fundamentals. So…what are plugins?
Plugins are packages of code (can be very small to very large) that when activated within WordPress, modify or extend the features and functionality for your site.
For clarification purposes, there are (basically) four components that work together to make WordPress work:
- WordPress core
- Themes
- Plugins
- MySQL database
Whenever you want to do something cool or when you have problems these are basically the only areas of interest. It is worth noting that numbers 1 and 4 are basically never tampered with directly (unless you are super advanced or out of your mind).
Lets make a plugin
WordPress is built on PHP so that language is the basis for our plugins. If you are not familiar with PHP but have experience in other languages, this should be pretty easy for you at the basic levels. If you have no programming experience at all, this may be a little challenging but I’m confident that you can do it.
For starters, to make this as easy as possible, I created a starter plugin that should give you a great head start.
https://github.com/brashrebel/starter-plugin
All you have to do is unzip the file and begin editing files within. I recommend a program like Sublime Text but some other good options could be:
- Notepad
- Notepad ++
- Bluefish Editor
- Smultron
Upload that unzipped folder to the wp-content/plugins directory and you are ready to get moving.
Lets check out the code
For starters you’ll see that I created a couple files and a couple folders. Not every plugin requires this much stuff. In fact, many basic plugins are just a single PHP file. However, it is good practice to segment your code in a logical way so that it is easier to modify, troubleshoot and understand later.
The first file we’ll look at is the starter-plugin.php file. You’ll see that this file starts with a comment. This is an essential part of every plugin. It tells WordPress “Hey, we gotta plugin here” so it can display it as an installed plugin in your dashboard and so you can activate it.
<?php /* Plugin Name: Starter Plugin Description: If you want to make a plugin, this gives you a great starting point. Version: 1.0 Author: Kyle Maurer Author URI: https://realbigmarketing.com/staff/kyle */
Below that you’ll see some more descriptive comments that are my explanations of whats going on.
The next thing you’ll see is:
require_once (dirname(__FILE__).'/admin/admin.php'); require_once (dirname(__FILE__).'/shortcodes.php');
What this does is grabs a couple of the other files included in this plugin so that they can be used as well. Just throwing files into our plugin’s directory isn’t enough, we need to include them as above.
It is worth noting that one could also use ‘include’ or ‘include_once’ instead of ‘require_once’ here. I found a great explanation of the difference between ‘require’ and ‘include’.
Next you’ll find some code for including a stylesheet. Notice that the ‘add_action’ line is commented out since I don’t actually want to enqueue this stylesheet if this plugin gets inadvertently activated.
//add_action('wp_enqueue_scripts', 'my_styles'); function my_styles() { //This is for setting an optional condition for when you want to include your style (if you don't want it everywhere) global $post_type; //variable for getting current post type if needed if ($post_type == 'my-post-type' || is_singular('my-post-type')) : //Now we actually register the stylesheet wp_enqueue_style("starter-plugin", plugins_url("/css/style.css", __FILE__), FALSE); endif; }
The last thing in the main file is a function for including some Javascript. Just like with stylesheets, it is appropriate to enqueue your scripts properly so that they are output in the right place and at the right time.
//add_action('wp_enqueue_scripts', 'my_cool_script'); function my_cool_script() { wp_enqueue_script("coolscript", plugins_url("/js/script.js", __FILE__), FALSE); }
The other files
I won’t bother to go through all the other files as they are mostly just placeholders with nothing in them but maybe a comment. I’ll mention the shortcodes one though.
I often write quick, little shortcodes for content related items I, or other authors, may use frequently. So many of my plugins have a shortcodes file. Here’s the example one from the starter plugin:
function my_shortcode() { return "Three is the number to which thou shalt count."; } //This part first creates a shortcode, then names the function that gets run when we use this shortcode add_shortcode('yippee', 'my_shortcode');
One thing worth noting, for those familiar with PHP, is that ‘return’ is used to output content in shortcodes instead of echo. You can read the codex on shortcodes for more detail.
Taking it further
Well, now you have the basics of writing a plugin. The next step is…to think of some plugins to create. There are a million-trillion possibilities (fact!) for making WordPress plugins. Here are some other suggestions and notes:
- When adding basic functionality to your site, the option of adding your code to your theme’s functions.php file OR creating a plugin will typically be a consideration. Generally speaking, you would only use the theme option if your function is ONLY useful with the current theme AND you have little to no interest in reusing it on other sites later.
- WPSnipp is a fantastic resource for lots of simple scripts that I think are perfect for plugin beginners. Just grab one of the many useful snippets published, modify to suit your needs and then drop it in your plugin. Boom. Done.
- WPBeginner, DoItWithWP, WP Tuts+, CSS Tricks and DigWP are just a few of my favorite resources for cool scripts that can inspire and empower you.
- If you manage to create a plugin that works well for you, you might consider publishing it on the WordPress Plugin Repository so that others can benefit as well. Just go here to submit one. The requirements are…not really a big deal. If it works for you and there is a chance others might also find it useful, I’d recommend just publishing it. Other users will be able to notify you of bugs and possible features to add. Its really a win-win.
I hope this was helpful to some. I’d love it if anyone shared comments with ideas on how this could be made easier as well as other tips for first time plugin developers.
Great tutorial! Do you offer, or know of affordable plugin development services? Cool resource you’ve got here.
Sure Lawrence. We do custom WordPress plugin development here and also have other acquaintances who are capable as well. Feel free to describe your needs.
Hello Kyle,
I have installed a localhost on my computer to try a get the plugins working on wordpress but i am pulling my hair out trying to work out why wp plugins will not work after they have clashed with each other. They worked fine by themselves, but since the clash there seems to be an error or something in the files some were.
Know matter what i do i just can not get them to work again I have deleted and reinstalled the plugins but the script wont even work with no other plugins in the directory.
I have even done a re-installation of wordpress
The backend works fine, it is the script on the frontend that is not working.
I have come across your sight after about 2 days of searching for a solution. i compelled to ask you for any advise that you may have to share.
One thing that i do know is that more than 2 scripts need to have a load order, but don’t know how to implement it on wordpress to over come the problem.
Michael,
I’d love to help you work through this problem. What plugins are you trying to run that won’t work? Do ANY plugins function?