Building a WordPress Plugin
We are a team of CakePHP Developers because nearly 95% of our work is CakePHP Development, however WordPress is the other 5% and when we use it, we want it to be just as easy to develop for as CakePHP.
So, let’s talk about customizing a WordPress site, there are three ways of doing it “the right way”. A plugin, a theme or a child theme. Each of these is primarily helpful at changing different aspects of your WordPress site. A plugin works well at customizing and expanding the features of WordPress to meet your particular needs.
We have created a small and simple plugin to walk through building a plugin and showing off what one can do.
A plugin needs only one thing to work, a PHP file (named anything) in a unique folder in the wp-content/plugins/ directory. Typically the file and folder will be similarly named as the plugin, though it isn’t required by any means. Next just start writing code to add whatever funcationalilty you need.
There are three main ways that plugins can interact with WordPress to add new features or extend any pre-existing features.
- Actions
- Filters
- Shortcodes
Actions
Actions are triggered when a specific action occurs on the WordPress site, publishing a post, the footer or head section is being output to the screen, etc. You can call your unique function when an action occurs and either output or perform some other action at that time. For instance, you can print something in the footer or email somebody whenever a post is published.
The code for this is straightforward:
add_action( 'action_name', 'function_to_call' );
Filters
Filters are run anytime that text is sent to either the database or to the browser, image caption output, media uploaded, or displaying the title.
Following in the footsteps of the Action API, code to tie into a filter is:
add_filter( 'filter_name', 'function_to_call' );
Shortcodes
Shortcodes are short snippets of code that can be called at any point in a WordPress template. Drop them into a page or add a shortcode to a template.
Want to guess the pattern for creating a shortcode?
add_shortcode( 'shortcode_name', 'function_to_call' );
To display a shortcode in a page or post simply:
[shortcode_name]
Shortcodes have a few advantages over filters and actions, in that you can also pass attributes to a shortcode and enclose content in a shortcode. So you can for instance use a shortcode to display a PayPal button with a custom plugin on every page that has a sellable product and pass in the specific product id and price. Or you can enclose a bit of text that you wish to turn into a custom span.
Loadsys Sample Plugin
Loadsys is providing a MIT licensed basic plugin to demonstrate the basics of building a plugin and how to use a plugin to add features to your WordPress site. Feel free to install and hack away at this code to build your own plugins.
The code can be downloaded or cloned from here:
https://github.com/loadsys/WordPress-Sample-Plugin
Justin Yost
Web Developer
Loadsys