Wednesday, June 26, 2013

How to Write a Basic WordPress Plugin

Plugins for WordPress can be very elaborate and require significant programming expertise to develop.  But learning to develop a WordPress plugin doesn't have to be difficult; you can start small and grow your skills over time. Let's start with a very simple WordPress plugin that you might build for yourself or a client.




Name the Plugin File and Subdirectory

Filename and Directory Structure for "Thanks for Reading" WordPress Plugin
Filename and Directory Structure for "Thanks for Reading" WordPress Plugin
Start by naming your plugin. We'll call this one "Thanks for Reading." You'll name your PHP file thanks-for-reading.php and by convention put in a /thanks-for-reading/ subfolder of the WordPress /wp-content/plugins/ directory.

Name using Lowercase and Underscores

TIP: When you name your files and directories we recommend you always use lowercase and dashes, never proper case nor underscores. "When in Rome" as they say. So please don't name your plugin files ThanksForReading.php, or worse Thanks_for_Reading.php.

Check Name in the WordPress Plugin Repository

But make sure there isn't a same-named plugin in the WordPress plugin repository by testing your desired plugin directory name on the end of the URL for plugins at wordpress.org to minimize potential future conflict with an existing plugin or if you ever want to publish yours:
http://wordpress.org/extend/plugins/thanks-for-reading
At the time of these writing there was no thanks-for-reading plugin in the WordPress plug repository. So far so good.
Plugin Not Found on WordPress.org
This is what we want to see.

Add a Plugin Header

Next add a plugin header to thanks-for-reading.php so WordPress can recognize your plugin. A plugin header is a simple PHP comment at the top of the file that minimally only needs the text Plugin Name: followed by the name of your plugin that you want end users to see:
  1. <?php
  2. /*
  3.  * Plugin Name: Thanks for Reading
  4.  */
Where your plugin appears in the WordPress Admin
How the "Thanks for Reading" plugin appears in the WordPress plugin admin section

Using "Hooks" to Modify a WordPress Site's Behavior

WordPress provides a system of "hooks" that allow plugins at selected points to generate output or modify variable state via "actions" and/or to modify data used by WordPress via "filters." Learning which hooks are available and how you use each one can take years but again you can learn plugin development in baby steps, one hook at a time.

Write your Filtering Function

The actual work of the plugin is contained in your function. Here's our proposed function:
  1. function the_content( $content ) {
  2.   return $content . '<p>Thanks for Reading!</p>';
  3. }
The function above receives the HTML for the post body in the parameter $content to which it add the HTML snippet '<p>Thanks for Reading!</p>' before returning the new value back to WordPress. And that's all the function needs to do. Except...

Add a Hopefully-Unique Prefix

Unfortunately the function name the_content() conflicts with an existing function in WordPress so the above doesn't work. To address these conflicts be sure to prefix your function names. You'll want something hopefully unique and not likely to conflict with function names other plugins or themes might use. We'll use tfr which is an initialism for "Thanks for Reading":
  1. function tfr_the_content( $content ) {
  2.   return $content . '<p>Thanks for Reading!</p>';
  3. }

Add the Filter Hook

Now that we have our function we can add a call to the WordPress add_filter() function where the first value passed is the hook name 'the_content' and the second value passed 'tfr_the_content' is the function name:
  1. add_filter( 'the_content', 'tfr_the_content' );

  2. function tfr_the_content( $content ) {
  3.   return $content . '<p>Thanks for Reading!</p>';
  4. }
And that's all there is to it.

The Complete Plugin

Here's the big picture, albeit admittedly quite small in this case:
  1. <?php
  2. /*
  3.  * Plugin Name: Thanks for Reading
  4.  */
  5. add_filter( 'the_content', 'tfr_the_content' );

  6. function tfr_the_content( $content ) {
  7.   return $content . '<p>Thanks for Reading!</p>';
  8. }
See what this post looks like with the Thanks for Reading plugin activated:
Output generated by our plugin.
Here's the output generated by our basic "Thanks for Reading" plugin.

Related Links

There are other tutorials on the web about writing WordPress plugins which cover ground that this post does not:
We're not necessarily endorsing all the techniques in those tutorials since we've not read each of them in-depth, we're just telling you they exist. Caveat emptor.

Summary

As you can see writing a WordPress plugin can be really simple. This example was deliberately trivial and ignored many aspects of WordPress plugin development but you've got to start somewhere. We have future plans to write in detail about all the aspects of WordPress plugin development that this post ignored

0 Responses to “How to Write a Basic WordPress Plugin”

Post a Comment

All Rights Reserved WP-Tutorial | Blogger Template by REFES Adda