When giving support or offering various snippets we frequently advise people to create a WordPress plugin to handle all of their modifications. Here is how you create a very simple plugin to house all your little snippets and modifications.
Basic Structure
A WordPress plugin can consist of as little as a single PHP file however for consistency I always create folder and at least the single PHP file within it. The folder and the file should have the same name with the exception of the file extension. We’re going to call our plugin “My Custom Functions” so the directory and file would be named accordingly.
- Folder:
example-plugin
- File:
example-plugin.php
When we’re done we’re going to upload this newly created folder with the file inside it to our wp-content/plugins
directory on our server.
Basics of a WordPress Plugin file
There are no doubt endless conversations that could be had about all the things that can go into your plugin file but I’m going to cover the absolute necessities to get you started quickly.
WordPress Plugin Header
Here is all that needs to be in your plugin file for WordPress to recognize it once uploaded.
<?php /* Plugin Name: Example Plugin */
See how easy that is? At this point you have a plugin that you can activate in your WordPress plugins area. Of course our plugin doesn’t actually do anything yet but the point is you have laid the foundation of your very own plugin and it was super easy.
Now there are other elements that you can include in this header. Things like a description, version, author, etc. You can read more about those here: http://codex.wordpress.org /Writing_a_Plugin #File_Headers
The rest of your plugin
As I said earlier there is really no end to what you can place inside your plugin but at this point, at a very basic level, you can think of it like your themes functions.php file. By that I mean that if you did nothing else you could place all those little code snippets that you love so much into this file instead of in your functions.php file and they would work exactly the same way.
As an example consider this little snippet that I sometimes use when I want to very quickly be able to redirect a page completely to a different page whether on the same site or another site entirely.
function my_custom_redirect () { global $post; if ( is_page() || is_object( $post ) ) { if ( $redirect = get_post_meta($post->ID, 'redirect', true ) ) { wp_redirect( $redirect ); exit; } } } add_action( 'get_header', 'my_custom_redirect' );
This snippet is very glamorous but what it essentially does is allow me to add a piece of custom post meta to any page called “redirect” with a URL. When the page is called and has this custom post meta then it’s automatically redirected to the new URL. Let take a look at it altogether now.
<?php /* Plugin Name: Example Plugin */ function my_custom_redirect () { global $post; if ( is_page() || is_object( $post ) ) { if ( $redirect = get_post_meta($post->ID, 'redirect', true ) ) { wp_redirect( $redirect ); exit; } } } add_action( 'get_header', 'my_custom_redirect' );
As you can see this plugin is not complicated at all and the best part is if something seems to be going fishy with my redirects I don’t have to dig through a huge file of miscellaneous functions to find it. In fact I could have named this plugin My Custom Redirect and then if anything happened I could just deactivate this one plugin without having an adverse affect on my entire site.
In Summary
You can no longer say that it’s to difficult to create a plugin. It’s just as easy as adding code to your functions.php, which you should almost never do, and so much cooler too.
Bonus Round
If you are interested in digging into creating WordPress plugins more complicated than what I describe above consider checking out the following resources.
- Writing a Plugin – the WordPress Codex
- WordPress Essentials: How To Create A WordPress Plugin
- Plugin Development 101 – Pippin Williamson – These is an ongoing series that requires a membership but I think you will be hard pressed to find a more thorough series.
sludger67 says
Excellent post, James. Quick, insightful and to the point. Have to admit, I was frightened off by some pages proclaiming to offer simple advice about how to build a plugin. Your article does entirely the opposite of this and acts as a perfect, non-threatening taster for absolute beginners like me. Thanks for this.
James Laws says
That’s the best compliment I could have received for this. Thank you for the kind words.
Mustafa says
Thank you very for this incredibly simple, elegant and easy to follow tutorial. I never knew a plugin can be created this easily. I have been adding lines to my functions.php until I read about why we shouldn’t change functions.php in one of your other tutorials. I will be creating my own plugins from now on.
Thanks 🙂
ganesh says
Thanks James,
My first plugin started with your tutorial, Please keep posting good stuffs.
I will be following your future tutorials.
Claudia says
Great post! I am working on my first plugin because you and Kevin have been so helpful, I wish there were more companies like you around. Please keep the good work!
James Laws says
Thanks so much, Claudia. We appreciate it and love helping people in any way we can. Glad you have found us to be helpful. 🙂
Ramandeep kaur says
Thank you so much James!!!
Excellent way of making a new plugin!!!
Tammy Wilson says
This was perfect! I was able to create my first plugin following you examples and it actually worked the first time-sweet!
Michal says
Thanks, I was looking for it! Simple and working
Jhon Marc B. Mamuyac says
Can you please give me an examples of plugins that are not yet existed in wordpress? 😀 thank you in advance Sir.
Kurt Sanger says
James;
Thank you for a great basic introduction to building my own plugin. Up to now I’ve been hacking / adding to the basic Word Press code. Which of course gets overwritten on every update.
Kurt.
Danish Ali says
Thanks James it really awesome start. (y) 🙂
Mubeen says
Best and easy steps tutorial on wordpress plugin developement. I just started the plugin developement and this post is really helpful for me .
Luke says
Thank you so much. I was able to make a simple Auto-Generated WordPress User ID Plugin.
Adnan Ali says
hi James,
I created a simple wp plugin to show some records from database plugin is working perfectly but after activating wp image manager is not working !!!!!!!!
please help me out
thanks in advance
Lars says
Wau .. I’ve just made my first plugin 🙂
I wanted the Ninja Form Error message to in the bottom …. and it was EASY
I did read this article first : http://docs.ninjaforms.com/customer/portal/articles/1981029
THANK YOU
From Lars, Copenhagen
Nishi says
Awesome Article! This post help me to make my first plugin.
Guido Boogaard says
A very good explanation, exactly what I wanted to know.
Thank you!
Bachot Mpunga says
To the point James
Thanks, this is the greatest and best way to teach a beginner, no mumbo jumbo.
I wish if all the coder out there could start to do the same thing.
Irfan Razzaq says
Thanks for sharing 🙂