WordPress Plugin Development

WordPress Plugin Development

If you can write WordPress plugins, you can make WordPress do just about anything.
From making the site easier to administer, to adding the odd tweak or new feature, to
completely changing the way your blog works; plugins are the method WordPress offers
to customize and extend its functionality. This book will show you how to build all sorts
of WordPress plugins: admin plugins, Widgets, plugins that alter your post output,
present custom “views” of your blog, and more.

This book focuses on teaching you all aspects of modern WordPress development and
usage. The book uses real and published WordPress plugins and follows their creation
from the idea to the finishing touches, in a series of carefully picked, easy-to-follow
tutorials. You will discover how to use the WordPress API in all typical situations, from
displaying output on the site in the beginning to turning WordPress into a CMS in the last
chapter. In Chapters 2 to 7, you will develop six concrete plugins and conquer all aspects
of WordPress plugin development.

Each new chapter and each new plugin introduces different features of WordPress and
how to put them to good use, allowing you to gradually advance your knowledge. This
book is written as a guide to take your WordPress skills from the very beginning to the
level where you are able to completely understand how WordPress works and how you
can use it to your advantage.

This is a Packt Beginners Guide, which means it focuses on practical examples and has a
fast-paced but friendly approach, with the opportunity to learn by experimentation and
play. Each chapter builds a practical plugin from the ground up using step-by-step
instructions. Individual sections show you how to code some functionality into your
plugin and follow up with a discussion of concepts.

What This Book Covers

Chapter 1 teaches the advantages of WordPress development, and what WordPress has to
offer to plugin authors.

Chapter 2 creates a working, useful, and attractive WordPress plugin from scratch. It
shows how to extract information using the WordPress API and how to use CSS to
improve the look of our plugin.

Chapter 3 explores more cool things we can do with WordPress by livening up the
default WordPress blogroll. The purpose of the plugin is to display the most recent posts
from the sites listed in the blogroll using a nice pop-up window.

Chapter 4 uses the mixed approach, by taking advantage of creative WordPress and
JavaScript techniques, in order to create an Ajax powered ‘Wall’ for your blog’s sidebar.
It introduces quite a few interesting techniques such as Widgets, interacting with the
WordPress Database, and Ajax form submission.

Chapter 5 covers the creation of a very sleek and stylish looking WordPress
enhancement. The purpose of the Snazzy Archives plugin will be to present your site
archives in a unique visual way. It shows how to manipulate the layout of the template
using shortcodes and custom templates.

Chapter 6 is all about digging a little deeper into WordPress and hacking the Write Post
screen. It shows how to create custom panels in the various sections of the Write Post
screen. It teaches how to access the current WordPress rich text editor, tinyMCE, and
create a button on its toolbar.

Chapter 7 explores the possibilities of turning WordPress into a Content Management
System (CMS), using methods provided to us by WordPress. It shows how to modify the
Manage Posts panel to display the information we want. It also covers managing who can
use your plugin by looking at the logged in user capabilities.

Chapter 8 covers the additional steps involved in localizing, documenting, publishing,
and promoting your plugin. It also covers useful tips and ideas to improve your general
WordPress knowledge further.

Social Bookmarking

I hope the first chapter got you warmed up and prepared for WordPress plugin development,
and that you are as eager to start as I am.

In this chapter, we will create our first functional WordPress plugin and learn how to interact
with the WordPress API (this is the WordPress interface to PHP) on the way. The knowledge
you will gain in this chapter alone will allow you to write a lot of similar plugins.

Let’s get moving! In this chapter, you will learn:

  • Creating a new plugin and having it displayed in the plugins admin panel
  • Checking the WordPress version and control activation of the plugin
  • Accessing API features—for example the title and permalink URL of each post
  • Using WordPress hooks to execute your plugin code when it’s needed
  • Using conditional tags to control the fl ow of your plugins

You will learn these by:

  • Creating a ‘social bookmarking’ type of plugin that adds a Digg button to each post
    on your blog
  • As you probably know, Digg is a very popular service for promoting interesting content on
    the Internet. The purpose of a Digg butt on on your blog is to make it easier for Digg users to
    vote for your article and also to bring in more visitors to your blog.

    The plugin we’ll create in this chapter will automatically insert the necessary code to each of
    your posts. So let’s get started with WordPress plugin development!

    Plugging in your first plugin

    Usually, the first step in plugin creation is coming up with a plugin name. We usually want to
    use a name that is associated with what the plugin does, so we will call this plugin, WP Digg
    This
    . WP is a common prefix used to name WordPress plugins.

    To introduce the plugin to WordPress, we need to create a standard plugin header. This
    will always be the first piece of code in the plugin file and it is used to identify the plugin
    to WordPress.

    Time for action – Create your first plugin

    In this example, we’re going to write the code to register the plugin with WordPress ,
    describe what the plugin does for the user, check whether it works on the currently installed
    version of WordPress, and to activateit.

    1. Create a file called wp-digg-this.php in your favourite text editor. It is common
      practice to use the plugin name as the name for the plugin file, with dashes ‘-‘
      instead of spaces.
    2. Next, add a plugin information header. The format of the header is always the same
      and you only need to change the relevant information for every plugin:
    3. 
      	<?php
      	/*
      	Plugin Name: WP Digg This
      	Version: 0.1
      	Description: Automatically adds Digg This button to your posts.
      	Author: Vladimir Prelovac
      	Author URI: http://www.prelovac.com/vladimir
      	Plugin URI: http://www.prelovac.com/vladimir/wordpress-plugins/
      	wp-digg-this
      	*/
      	?>
      

    4. Now add the code to check the WordPress version:
    5. 
      	/* Version check */
      	global $wp_version;
      	$exit_msg='WP Digg This requires WordPress 2.5 or newer.
      	<a href="http://codex.wordpress.org/Upgrading_WordPress">Please
      	update!</a>';
      	if (version_compare($wp_version,"2.5","<"))
      	{
      		exit ($exit_msg);
      	}
      	?>
      

    6. Upload your plugin file to the wp-content/plugins folder on your server using
      your FTP client.
    7. Go to your WordPress Plugins admin panel. You should now see your plugin listed
      among other plugins:
    8. This means we have just completed the necessary steps to display our plugin in
      WordPress. Our plugin can be even activated now—although it does not do
      anything useful (yet).
    9. What just happened?

      We created a working plugin template by using a plugin information header and the
      version check code. The plugin header allows the plugin to be identified and displayed
      properly in the plugins admin panel. The version check code will warn users of our plugin
      who have older WordPress versions to upgrade their WordPress installation and prevent
      compatibility problems.

      The plugin information header

      To identify the plugin to WordPress, we need to include a plugin information header with
      each plugin.

      The header is writt en as a PHP comment and contains several fields with
      important information.

      This code alone is enough for the plugin to be registered, displayed in the admin panel and
      readied for activation.

      
      	If your future plugin has more than one PHP file, the plugin information
      	should be placed only in your main file, the one which will include() or
      	require() the other plugin PHP files.
      

      Checking WordPress versions

      To ensure that our plugin is not activated on incompatible WordPress versions, we will
      perform a simple WordPress version check at the very beginning of our code.

      WordPress provides the global variable $wp_version that provides the current WordPress
      version in standard format. We can then use PHP function version_compare() to
      compare this and our required version for the plugin, using the following code:

      
      	if (version_compare($wp_version,"2.6","<"))
      	{
      		// do something if WordPress version is lower then 2.6
      	}
      

      If we want to stop the execution of the plugin upon activation, we can use the exit()
      function with the error message we want to show.

      In our case, we want to show the required version information and display the link to the
      WordPress upgrade site.

      
      	$exit_msg='WP Digg This requires WordPress 2.6 or newer. <a
      	href="http://codex.wordpress.org/Upgrading_WordPress">Please
      	update!</a>';
      	if (version_compare($wp_version,"2.6","<"))
      	{
      		exit ($exit_msg);
      	}
      

      While being simple, this piece of code is also very eff ective. With the constant development
      of WordPress, and newer versions evolving relatively often, you can use version checking to
      prevent potential incompatibility problems.

      The version number of your current WordPress installation can be found in the footer text
      of the admin menu. To begin with, you can use that version in your plugin version check
      (for example 2.6).

      Later, when you learn about WordPress versions and their diff erences, you'll be able to
      lower the version requirement to the minimal your plugin will be compatible with. This will
      allow your plugin to be used on more blogs, as not all blogs always use the latest version of
      WordPress.

      Comments

      comments

      Pages: 1 2 3 4 5

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*