Develop a WordPress plug-in with Eclipse PDT

Use Eclipse tools to extend WordPress

WordPress is a Web publishing platform written in PHP, using MySQL for storage. It provides extensibility by building plug-ins that add filters and actions. The Eclipse PHP Development Tools (PDT) V2.0 project allows you to use Eclipse to build PHP applications. Learn how to extend WordPress by using PDT to build plug-ins.

Nathan A. Good, Senior Information Engineer, Freelance Developer

Nathan Good lives in the Twin Cities area of Minnesota. Professionally, he does software development, software architecture, and systems administration. When he's not writing software, he enjoys building PCs and servers, reading about and working with new technologies, and trying to get his friends to make the move to open source software. He's written and co-written many books and articles, including Professional Red Hat Enterprise Linux 3, Regular Expression Recipes: A Problem-Solution Approach, and Foundations of PEAR: Rapid PHP Development.



10 March 2009

Also available in Russian Japanese

The Eclipse PDT V2.0 project was recently released. It includes many new usability features over the previous version, including the ability to add a build path, open types, and access improved code assistance.

This article demonstrates how to download and install WordPress, and how to use Eclipse PDT to build a plug-in. It also demonstrates how to export the plug-in from Eclipse so it can be shared.

To get the most out of this article, you need to have the following tools:

  • PHP V4.3 or later
  • MySQL V4.0 or later
  • Eclipse V3.4 with PDT

An overview of WordPress

WordPress is a publishing platform written in PHP that allows you to quickly and easily publish content to the Web. It is highly extensible, allowing you to create plug-ins that provide nearly any functionality. Since 2005, millions of copies of it have been downloaded and used by large companies and individuals alike.

WordPress is easy to install and configure, and the requirements are few: only PHP and MySQL. To install WordPress, simply download the ZIP file from the Web site (see Resources) and extract the ZIP to a directory accessible from your Web server. In this article, I'm using a Web server configured to allow publication of the user directory (like the userdir module in Apache) to the Web server. Rename the directory, if you like: I changed mine from wordpress to blog.

After you finish extracting the files and renaming the directory, follow the "Famous 5-Minute Install" instructions (see Resources). If you have an Internet Service Provider (ISP) that supports WordPress's requirements, you can develop locally and FTP the entire directory to your site.


Extending WordPress

Many people use WordPress for blogging, although it's good for publishing any content easily. I've worked on sites that use WordPress as the main engine to host informational sites about products that include forums, feedback forms, and Adobe® Flash® animation on the home page.

Many plug-ins are available for WordPress (see Resources). These plug-ins range from the ability to add custom fields that collect more information about the WordPress site's registered users to tag clouds, antispam utilities, and contact plug-ins that generate "Share this" links to popular sites like Facebook, MySpace, del.icio.us, and Technorati.

But even with all these plug-ins, there may be cases where you need to extend WordPress to fit your own needs. The rest of this article shows how you can use Eclipse and PDT to set up a development environment in which you can develop and test WordPress plug-ins.


Setting up your workspace

Before setting up your workspace, make sure you have WordPress installed and running. You will need to have read and write access to the WordPress directory. The best scenario is to have user directories configured on the Web server on your machine so you can put the files in your own home directory. But a shared directory also works as long as you can read and write to it.

Before proceeding, create a directory inside the WordPress_home/wp-content/plugins directory (where WordPress_home is the full path to the directory into which you extracted WordPress) — for example, WordPress home/wp-content/plugins/myplugin. In just a few moments, that directory will be home to your new WordPress plug-in.

If you have WordPress up and running, and can read and write to the directory you just created for your plug-in, you are ready to create a new project for your WordPress plug-in:

  1. Choose File > Project, then select PHP Project from the Wizards list, as Figure 1 shows.
    Figure 1. Creating your new PHP project
    Creating your new PHP project
  2. Click Next.
  3. In the New PHP Project window, type the name of your plug-in in the Project name box.
  4. Under Contents, select Create project from existing source. Then browse to or type the name of the directory that you just created (for example, my directory is /Users/ngood/Sites/blog/wp-content/plugins/myplugin) in the Directory box, as Figure 2 shows.
    Figure 2. Defining the project location
    Defining the project location
  5. Click Finish when you've entered the directory information, as the rest of the information is left at the default values.

After creating the project, the directory listing of the your WordPress_home/wp-content/plugins/myplugin directory will look like Listing 1.

Listing 1. The contents of the new myplugin directory
drwxr-xr-x  7 user  user    238 Feb  3 11:02 .
drwxr-xr-x@ 5 user  user    170 Feb  3 08:39 ..
-rw-r--r--  1 user  user    259 Feb  3 11:10 .buildpath
-rw-r--r--  1 user  user  30244 Feb  3 11:02 .pmd
-rw-r--r--  1 user  user    496 Feb  3 08:39 .project
drwxr-xr-x  3 user  user    102 Feb  3 08:39 .settings
-rw-r--r--  1 user  user    795 Feb  3 11:13 myplugin.php

Working in a team environment

If you're working in a team environment and sharing your code, the Eclipse tools will take care of checking the files in from the correct directory into the source-control repository. However, take note of the directory in which the files are being imported from your team sharing environment into your workspace. When you are importing files from a source-control repository into your workspace, you can define where you want the files to be stored (see Figure 3). For more information about using Eclipse with source-code repositories, see Resources.

Figure 3. The plug-in as shown in the WordPress administrator pane
The plug-in as shown in the WordPress administrator pane

Adding WordPress to your build path

The PDT project provides the ability to add folders to your build path, which also gives you the ability to have code-completion tools work with WordPress. To be able to see the add_filter and add_action functions in code completion, you can modify the build path to add the WordPress wp-includes directory.

To add the WordPress wp-includes directory to your build path, choose Project > Properties with your PHP project highlighted. On the Libraries tab, click Add External Source Folder, then browse to the wp-includes directory found where you installed WordPress. Click OK when you're finished. Now you can type add_ into the PHP editor, invoke code completion, and see a list that includes add_action and add_filter.


Adding your first plug-in file

To add your first plug-in file, choose File > New > PHP File. The first file needs to be named the same as the directory in which the file resides. For example, if the plug-in directory is called myplugin, name the file myplugin.php. This will be the file WordPress reads to get information about your plug-in and to register your extension points.

For WordPress to identify your plug-in, you need to paste a comment block into your new plug-in file. If you're creating a lot of WordPress plug-ins, you may want to consider putting this code block in the code templates for PHP. See Listing 2 for an example of the completed code block.

Listing 2. The comment block to identify your plug-in
/*
Plugin Name: My Plugin
Plugin URI: http://www.example.com/myplugin
Description: This is a really great plugin that extends WordPress.
Version: 1.0.0
Author: Nathan A. Good
Author URI: http://www.nathanagood.com
*/

Right now, your plug-in doesn't do anything. But with only the comment block, it will be displayed in WordPress's Plugins administrative area. To view your new plug-in, log in to your WordPress instance as an administrator, then click Plugins. You should see your new plug-in listed under the Inactive Plugins.

Figure 4. Defining the source location when importing from the source-control repository
Defining the source location when importing from the source-control repository

Adding functionality

In this article, I add two pieces of functionality. The first is a simple one that makes the blog entry title uppercase. That is an example of adding a filter. The second is an extra field on the end of the registration form for a user. That is an example of adding an action.

I'm going to write the examples in functional code to keep them as brief as possible. Remember: If you're using PHP V5 or later, you can incorporate object-oriented concepts to make your code easier to read and maintain. For more information about writing more maintainable PHP by using object-oriented programming concepts and design patterns, see Resources.

The uppercase title

The code for the title filter is shown in Listing 3. In it, you can see that it uses the strtoupper() function in PHP to make the title of the blog post uppercase.

Listing 3. The function to make your title uppercase
/**
 * Capitalizes the title given by $title.
 */
function capitalizeTitle($title) {
    return strtoupper($title);
}

When you have written the function, you need to call add_filter to register your function with WordPress as the specified filter extension point. A link to the full list of the available filters is provided in Resources. An example of adding the filter is shown below.

Listing 4. Adding the filter with add_filter
/* now add the filter */
add_filter('the_title', 'capitalizeTitle');

Adding the registration fields

The code to generate a new custom registration field is shown in Listing 5. I used the Firebug extension to view the Cascading Style Sheet (CSS) styles of the existing registration form fields to make sure my custom fields had the same CSS style class.

Listing 5. The new custom field action
/**
 * Adds a custom field that prompts the user for their favorite
 * color.
 * @return void
 */
function drawCustomField() {
    echo '<p><label>Favorite Color:<br />';
    echo '<input autocomplete="off" class="input" name="fav_color" ';
    echo ' id="fav_color" size="25"';
    echo ' value="' . $_POST['fav_color'] . '" type="text" tabindex="32" />';
    echo '</label><br /></p>';
}

To add the custom action, use the code shown below.

Listing 6. Adding the action with add_action
/* now add the action */
add_action('register_form', 'drawCustomField');

This action simply draws the field on the form. It does not actually save the data when the user submits the form. To save the data, use the profile_update action. For a link to the full list of the available actions and what they do, see Resources.


Testing the plug-in

With the code put into the project, you're ready to test the plug-in. Before testing it, you need to enable the plug-in in WordPress. Log in to WordPress as the administrator and click Plugins. Find your plug-in, then click Activate.

When you have activated the plug-in, go to the home page of your blog. You should notice that the "Hello World!" sample post title is now all in uppercase letters. If you deactivate your plug-in, it will revert to normal capitalization. You can use this functionality to do other things, such as turn URLs found in titles into hyperlinks. You can add more than one filter with the same name, so instead of creating one filter that does different things, consider making small, concentrated functions and chaining them together.

To test the registration field part of the plug-in, you must log in to WordPress as the administrative user and turn on the feature that allows users to register on the Settings page. After enabling this option, you will see the Favorite Color field when you go to the registration page.


Other WordPress extension points

In addition to adding filters and actions, you can override functions in WordPress. The functions you can override are defined in the pluggable.php file in your WordPress home directory. One example is wp_mail, which gives you the opportunity to provide a different function for e-mailing your users. Many of the overridable functions deal with user authorization, such as logging in, setting cookies, and notification of different events. The ability to override authorization-based functionality allows you to build plug-ins that might provide alternative forms of authentication and authorization to WordPress, such as authentication against an enterprise user store.


Exporting the plug-in

After you have written and thoroughly tested your plug-in, you will probably want to share it with others. WordPress plug-ins are packaged like WordPress itself: Most are simply a ZIP file that is expanded and copied into the WordPress plugins directory.

You can use Eclipse to export the plug-in files. By using Eclipse to export the project to a ZIP archive file, you can avoid sending along project-specific files and team sharing directories and files (like .svn or Concurrent Version Systems (CVS) directories).

To export your plug-in to an archive file, select your PHP project, then choose File > Export. In the Select window, choose Archive File from the destination list, then click Next.

Figure 5. Exporting your WordPress plug-in
Exporting your WordPress plug-in

In the Archive file window, shown in Figure 6, click Deselect All, then click the PHP files you want to include in your archive file (such as myplugin.php). When you're finished selecting the PHP files, choose the location of the archive file from To archive file, then click Finish to create the file. To maintain consistency, it's best to keep the name of the ZIP file the same as the plugin directory and the main plug-in file.

Figure 6. Choosing files to export
Choosing files to export

Now that you've created the archive file from Eclipse, you can publish it on a Web server or copy it to your ISP's location so you can use it.

To add information about your plug-in on the official WordPress site, see Resources and follow the directions.


Conclusion

WordPress is an increasingly popular, extensible Web application that allows you to easily publish content to your users. You can extend WordPress to provide more functionality without having to modify any WordPress code.

The Eclipse IDE with PDT installed allows you to build a plug-in using PHP tools. You can test and run your plug-in with WordPress until it is complete. Then you can use the Eclipse export functionality to export the plug-in to an archive file suitable for distribution.

Resources

Learn

Get products and technologies

Discuss

  • The Eclipse Platform newsgroups should be your first stop to discuss questions regarding Eclipse. (Selecting this will launch your default Usenet news reader application and open eclipse.platform.)
  • The Eclipse newsgroups has many resources for people interested in using and extending Eclipse.
  • Participate in developerWorks blogs and get involved in the developerWorks community.

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Open source on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=374168
ArticleTitle=Develop a WordPress plug-in with Eclipse PDT
publish-date=03102009