Shortcode API in WordPress is really cool. I already wrote about coding your very first shortcode in WordPress. Today’s discussion is about using your WordPress knowledge in the best possible way. Today I’ll be creating the same three shortcodes we created in the earlier article, but this time we will be coding them through a WordPress plugin.
In a WordPress install, there is a folder wp-content which contains both the plugins & themes folders. If you browse the path wp-content/themes/YourActivatedThemeDirectory/ you can always find a functions.php file here. Yes! in the root of your activated theme’s folder. One can easily put the code for shortcodes in this functions.php file to create a shortcode. I wrote about this method in coding your very first shortcode in WordPres.
The problem with this method is that whenever you change your theme you end up loosing all the shortcodes. Solution to which is hunting down all the shortcodes in old theme’s functions.php file and then re-adding them to the new theme’s functions.php file. Even a beginner can sense something fishy here.
When you create shortcodes in the form of a WordPress plugin, you never ever have to worry about them. Even when you change your theme, the plugin keeps on working, flawlessly. This is the right way of doing it.
So, if you are a freelancer/WordPress developer, then the very next time your client ask you to add some shortcodes in his project, do it through a plugin. I am starting a very basic plugin for shortcodes which I will keep updating to create a full-fledged boilerplate for shortcode plugins in WordPress. But for now, it only allows you to understand the basics of creating a plugin and then using standard, organized and modular approach to executing your needs.
Themes are used for presentation while plugins are responsible for the functionality relevant features in WordPress projects. Whenever you are building something in WordPress, think it through. Decide what is a presentation and what is functionality. Then decide what should and shouldn’t be there in theme’s functions.php file.
We are going to code a basic shortcodes plugin which will have a base plugin file and shortcodes’ module files in it. Before I start, let me tell you that the base file in a WordPress plugin acts similar to functions.php in your theme. This base file can have a custom name. It can execute functions in WordPress just like the functions.php file of a theme. So, the code of shortcodes is not going to be changed. It will be similar to what I wrote about in code your first shortcode in WordPress tutorial, this tutorial is more about where to add this code and how to add it.
Did you enjoy the maintainable standard WordPress plugin code with documentation? If you have any questions, don’t hesitate to ask. Pull requests and issues are most welcomed at the GitHub’s repository.
Takeaway my professional opinion on open-source, developer relations, growing your business with family, and everything in between! Delivered to your inbox a couple of times every year. I'm even funny at times. I hate spam — pinky-promise!