The Basics of WordPress and jQuery Plugin

0
364
9 min read

 

WordPress 3.0 jQuery

WordPress 3.0 jQuery

Enhance your WordPress website with the captivating effects of jQuery.

  • Enhance the usability and increase visual interest in your WordPress 3.0 site with easy-to-implement jQuery techniques
  • Create advanced animations, use the UI plugin to your advantage within WordPress, and create custom jQuery plugins for your site
  • Turn your jQuery plugins into WordPress plugins and share with the world
  • Implement all of the above jQuery enhancements without ever having to make a WordPress content editor switch over into HTML view

 

Read more about this book

(For more resources on WordPress and jQuery, see here.)

WordPress plugins overview

So themes change the look of WordPress without affecting its functionality. But what if you want to change or add functionality? WordPress plugins allow easy modification, customization, and enhancement to a WordPress site. Instead of having to dig in to the main files and change the core programming of WordPress, you can add functionality by installing and activating WordPress plugins.

The WordPress development team took great care to make it easy to create plugins using access points and methods provided by the WordPress’ Plugin API (Application Program Interface). The best place to search for plugins is: http://wordpress.org/extend/plugins/. The following is a screenshot of the WordPress plugin directory’s main page:

(Move the mouse over the image to enlarge.)

Once you have a plugin, it’s a simple matter of decompressing the file (usually just unzipping it) and reading the included readme.txt file for installation and activation instructions. For most WordPress plugins, this is simply a matter of uploading the file or directory to your WordPress installation’s wp-content/plugins directory and then navigating to the Administration | Plugins | Installed panel to activate it. The next screenshot shows the Plugins admin panel with the activation screen for the default Askimet, Hello Dolly, and new WordPress Importer plugins:

So how does a WordPress plugin differ from a jQuery plugin? In theory and intent, not that much, but in practice, there are quite a few differences. Let’s take a look at jQuery plugins.

jQuery plugins overview

jQuery has the ability to allow you to take the scripts that you’ve created and encapsulate them into the jQuery function object. This allows your jQuery code to do a couple of key things. First, it becomes more easily ported over to different situations and uses. Second, your plugin works as a function that can be integrated into larger scripts as part of the jQuery statement chain.

The best place to browse for jQuery plugins is the jQuery plugins page (http://plugins.jquery.com/), as seen in the next screenshot:

In addition to having jQuery already bundled, WordPress has quite a few jQuery plugins already bundled with it as well. WordPress comes bundled with Color, Thickbox as well as Form and most of the jQuery UI plugins. Each of these plugins can be enabled with the wp_enqueue_script either in the theme’s header.php file or function.php file, in WordPress. In this article, we’ll shortly learn how to enable a jQuery plugin directly in a WordPress plugin.

Of course, you can also download jQuery plugins and include them manually into your WordPress theme or plugins. You’d do this for plugins that aren’t bundled with WordPress, or if you need to amend the plugins in anyway.

Yes, you’ve noticed there’s no easy jQuery plugin activation panel in WordPress. This is where understanding your chosen theme and WordPress plugins will come in handy! You’ll soon find you have quite a few options to choose from when leveraging jQuery. Now that we have an overview of what WordPress themes, plugins, and jQuery plugins are, let’s learn how to take better advantage of them.

The basics of a WordPress plugin

The goal here is to show you the structure of a simple WordPress plugin and the basics of how to construct one. Understanding this, you can begin to write your own basic plugins and feel more confident looking through other people’s plugins when assessing what kind of features they provide to your WordPress site and if you need to tweak anything for your jQuery enhancements. Even as simply and basically as we’re going to work, you’ll see how truly powerful WordPress plugins can be.

Want to become a WordPress plugin rockstar?
You can start off with, yet again, WordPress 2.7 Complete by April Hodge Silver and Hasin Hayder. There’s a chapter on plugins that walks you through creating very useful simple plugins, as well as a more complex plugin that writes to the WordPress database. Beyond that, you’ll want to check out WordPress Plugin Development: Beginner’s Guide by Vladimir Prelovac. Don’t let the title fool you, Vladimir will have you generating feature rich and dynamic WordPress plugins using WordPress’ coding standards all explained with clear, step-by-step code.

Working with plugins does require some experience with PHP. I’ll keep this explanation fairly straightforward for non-PHP developers, and those of you with PHP experience should be able to see how to expand on this example to your advantage in WordPress.

Just as with themes, WordPress plugins require a little structure to get started with them. There’s no defined hierarchy for plugin files, but you do need, at the very least, a PHP file with a special comment up top so that WordPress can display it within the Plugin Management page. While there are some single-file plugins out there, such as the Hello Dolly plugin, which comes with your WordPress installation, you never know when you first start developing, the ways in which a plugin may grow. To be safe, I like to organize my plugins into a uniquely named folder. Again, like with themes, WordPress relies on the plugin directory’s namespace, so uniqueness is of key importance!

In the wp-content/plugins directory you can place a unique folder and inside that, create a .php file, and at the beginning of the file, inside the <?php ?> tags, include the following header information. Only the bold information is absolutely required. The rest of the information is optional and populates the Manage Plugins page in the Administration panel.

<?php
/*
Plugin Name: your WordPress Plugin Name goes here
Plugin URI: http://yoururl.com/plugin-info
Description: Explanation of what it does
Author: Your Name
Version: 1.0
Author URI: http://yoururl.com
*/
//plugin code will go here
?>

Make sure that you don’t have any spaces before your <?php tag or after your ?> tag. If you do, WordPress will display some errors because the system will get some errors about page headers already being sent.

Once you have your .php file set up in its own directory, inside your plugin directory, you can add a basic PHP function. You can then decide how you want to evoke that function, using an action hook or a filter hook. For example:

<?php
/*
Plugin Name: your WordPress Plugin Name goes here
Plugin URI: http://yoururl.com/plugin-info
Description: Explanation of what it does
Author: Your Name
Version: 1.0
Author URI: http://yoururl.com
*/
function myPluginFunction(){
//function code will go here
}
add_filter('the_title', 'myPluginFunction');
//or you could:
/*add_action('wp_head', 'myPluginFunction');*/
?>

If you didn’t have wp_head or wp_footer in your theme, many plugins can’t function, and you limit yourself to the plugins you can write. In my plugins, I mostly use wp_header and the init action hooks.

Luckily, most filter hooks will work in your plugins as WordPress will run through them in The Loop. For the most part, you’ll get the most work done in your plugin using the_title and the_content filter hooks. Each of these filter’s hooks will execute your function when WordPress loops through those template tags in the loop.

Want to know what filter and action hooks are available?
The list is exhaustive. In fact, it’s so immense that the WordPress codex doesn’t seem to have them all documented! For the most complete listing available of all action and filter hooks, including newer hooks available in version 2.9.x, you’ll want to check out Adam Brown’s WordPress Hooks Database: http://adambrown.info/p/wp_hooks.
Overwhelmed by the database? Of course, checking out Vladimir’s WordPress Plugin Development: Beginner’s Guide will get you started with an arsenal of action and filter hooks as well.

You now understand the basics of a WordPress plugin! Let’s do something with it.

Project: Writing a WordPress plugin to display author bios

As we’ve discussed, plugins can help expand WordPress and give it new functionality. However, we’ve seen that adding jQuery scripts directly to the theme and editing its template pages here and there will do the trick in most cases. But let’s imagine a more complicated scenario using our modified default theme and the hypothetical client.

While we tweaked the default theme, I figured that this client might want to have her site’s focus be more journalism oriented, and thus, she’d want some attention drawn to the author of each post upfront. I was right, she does. However, there’s a catch. She doesn’t just want their WordPress nickname displayed; she’d prefer their full first and last name be displayed as well, as it’s more professional. She’d also like their quick biography displayed with a link to their own URL and yet, not have that information “get in the way” of the article itself, or lost down at the bottom of the post. And here’s the really fun part; she wants this change affected not just on this site, but across her network of genre-specific news sites, over 20 of them at last count (dang, I forgot she had so many sites! Good thing she’s hypothetical).

For this specific WordPress site, it’s easy enough to go in and comment out the custom function we dealt with earlier: add in the_author tag and display it twice, passing each tag some parameters to display the first and last name. I can also add a tag to display the author’s biography snippet from the user panel and URL (if they’ve filled out that information). Also, it’s certainly easy enough to add a little jQuery script to make that bio div show up on a rollover of the author’s name. However, having to take all that work and then re-copy it into 20 different sites, many of which are not using the default theme, and most of which have not had jQuery included into their theme, does sound like an unnecessary amount of work (to boot, the client has mentioned that she’s deciding on some new themes for some of the sites, but she doesn’t know which sites will get what new themes yet).

It is an unnecessary amount of work. Instead of amending this theme and then poking through, pasting, testing, and tweaking in 20 other themes, we’ll spend that time creating a WordPress plugin. It will then be easy to deploy it across all the client’s sites, and it shouldn’t matter what theme each site is using. Let’s get started!

LEAVE A REPLY

Please enter your comment!
Please enter your name here