127
<! Static content could go above widgetized area. >
<?php if (!function_exists('dynamic_sidebar') ||
!dynamic_sidebar('Sidebar Widgets')) : ?>
Content in here will ONLY show if there are no widgets active,
or if the version of WordPress running doesn't support widgets.
Otherwise, this area is where the widgets go, set via the Admin
<?php endif; ?>
<! Static content could go below widgetized area. >
Step 2: Activate your widgetized area
In the functions.php file, use this function to activate the widgetized area:
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name'=>'Sidebar Widgets',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="widgettitle">',
'after_title' => '</h4>',
));
}
You’ll notice in this code that you reference the widgetized area by name and
then give it some HTML parameters for before/after the widget and before/after
the title. This gives you the chance to mimic your theme’s setup and gives you the
hooks you need to style the widgets as needed.
128
In the HTML, notice that each widget also contains a class and ID value, which gives
you the opportunity to style all the widgets as a group or target individual widgets
for styling.
Step 3: Managing your widgets
There are all sorts of potentially useful widgets built right into WordPress. Simply
drag them from the “Available Widgets” section over into the widget area to the
right. The area should be showing and properly labeled now. Once dragged over,
widgets typically have options that you can set, then save.
Note that many plugins operate by adding to this list of available widgets.
If the beauty of widgets still eludes you, consider the control it offers someone
managing a site who is somewhat comfortable with WordPress but not at all
comfortable with editing code. A widgetized area allows them to rearrange things,
edit text, change titles, change the number of things shown, and more, directly
through the Admin itself, without having to change one piece of code.
Coming up…
With a good understanding of the WordPress theme system under our belts, we
forge ahead and dig into the many ways that WordPress can be extended…
129
Komodo Media is a
stunningly beautiful site
with loads of attention to
detail. WordPress behind
the scenes? You bet.
/>Any suciently advanced technology is
indistinguishable from magic.
– ARTHUR C. CLARKE
Welcome to WordPress
131131
5.1.1 Extensibility
Out of the box, WordPress is a powerful platform for dynamic websites that may
be used to build a wide variety of projects. Especially for blog-style sites, WordPress
provides everything you need to establish yourself on the Web with a solid, fully
functional blog featuring everything from a beautiful, easy-to-use Admin interface
to a robust posting and commenting system that makes the process of posting
content and facilitating discussion a real breeze. Even so, one of the reasons why
WordPress has achieved its enormous popularity involves its vast extensibility.
5.1.2 Extending WordPress with Plugins
From themes and plugins to custom functions, scripts and even core hacks, the
degree to which WordPress enables users to customize its implementation is
astounding. In fact, with a few choice plugins, it is possible to transform WordPress
from a powerful blogging engine into a highly customized Content Management
System (CMS). In this chapter we examine some of best techniques and important
aspects for extending the functionality of WordPress.
5.1.3 A Plugin for (Almost) Everything
The easiest way to integrate additional functionality into WordPress involves
taking advantage of the thousands of plugins available at the WordPress Plugins
Directory and around the Web. Even when you exclude the many plugins not
5
Extending the Functionality
of WordPress
132
included in the official directory, there are still more than 10,648 plugins available
at the time of this writing (more than twice the number since the last update of
this book). With that many plugins available to you, the chances that you will find
one that suits your specific needs is very good indeed. Here are some of the more
popular categories of plugins featured at the Plugin Directory:
• widgets - 1769 plugins
• Post & posts - 2178 plugins
• administration (admin) - 948 plugins
• sidebar - 906 plugins
• comments - 703 plugins
• images - 584 plugins
• links - 525 plugins
And many, many more. Out of the box, WordPress includes two plugins, one
incredibly useful and the other relatively useless. The first of these default plugins
is Akismet, which is one of the best anti-spam plugins currently available for
WordPress. Akismet protects your comments by comparing them against their
constantly evolving database. Any comments that look like spam are blocked and
sent quietly to the spam bin. Users can then review the comments flagged as spam
or else let Akismet delete them automatically after 15 days.
Unfortunately, the other plugin that is included with WordPress, Hello Dolly, is
not nearly as useful. This plugin does nothing more than display a random lyric
from Jerry Herman’s song, “Hello Dolly,” in the upper-right hand corner of the
WordPress Admin screen. Hello Dolly was the very first plugin made for WordPress,
and will probably be included with WordPress until the end of time. Unless you’re
really sentimental or happen to love Louis Armstrong, this plugin is essentially
worthless and should be deleted as part of your configuration and set-up routine.
Of the thousands of plugins available for WordPress, there are a handful of plugins
that are installed on a large majority of sites and are considered by many to be
Goodbye Dolly?
In a poll at the DiW site,
WordPress users voted whether
or not the Hello Dolly plugin
should be included with
WordPress. The results? Of
over 1200 people who took the
poll, more than 1000 of them
(78%) voted that the plugin
should not be included
with WordPress.
/>133
“absolutely essential” for any WordPress installation. These plugins greatly increase
the power of WordPress in several important areas. See section 2.7.4 for our list of
essential plugins.
All of these plugins – and thousands more – are ready for download at the
WordPress Plugin Repository. There are far too many plugins to discuss or even
review in this book. Besides, many of the plugins that are available in the Codex
replicate the functionality of other plugins (Google Analytics, anyone?) and are
ultimately extraneous. Many plugins are no longer maintained and may only
work with older versions of WordPress; and conversely, many newer plugins are
developed exclusively for current versions of WordPress and are not compatible
with older versions.
As you set off to load up on plugins, keep in mind that around 99% of WordPress
plugins are created by independent, third-party developers and are not always
tested or optimized for maximum performance. Many are superfluous, providing
functionality that is easily achieved by simpler methods. In fact, chances are high
that you may not even need a plugin to achieve your design and development
goals. Before reaching for that easy plugin fix, ask yourself if installing another
plugin is indeed the best solution.
5.1.4 Do You Need a Plugin?
Once you have determined a need to expand or enhance WordPress’ default
functionality, determine whether or not a plugin is required to get the job done.
With WordPress, there is generally more than one way of doing things, especially
when it comes to theme-related modifications, layout modifications, and display
features. For example, if you would like to display the date and time of the most
recent post modification, you could install a plugin to do the job, or you could
simply add the following slice of code to the desired location in your theme file:
<p>Updated on <?php $x = get_the_time('U'); $m = get_the_modied_time('U');
if ($m != $x) { the_modied_time('F d, Y'); } ?></p>
Post-Modified Date
What does this code snippet
do? It displays the date/
time that the post was last
modied. Just place into the
loop (perhaps along with other
metadata) and the “post-
modied” date and time will
be displayed. For more code
snippets that replace plugins,
check out this article at
Perishable Press:
/>134
Custom Query String
/>CQS enables you to specify the
number of posts to display for
different types of page views. For
example, this plugin makes it possible
to show 10 posts on your home page,
20 posts for your archive pages, 50
posts for your search results, and so
on. Without this plugin, all of these
page views would simply display
the number of posts specified in
the Admin area (under Settings >
Reading).
The Excerpt Reloaded
/>the_excerpt_reloaded enables
you to completely customize the
excerpts that are displayed on your
site. Provides control over excerpt
size, type, format, allowed markup
elements, and much more. This
level of control is perfect for
displaying excerpts in multiple or
customized loops.
Theme Switcher
/>Perhaps the most underrated plugin
of them all, Theme Switcher enables
multiple themes on your site. This is
useful for enabling users to choose
alternate themes, but it is even more
useful for developing themes behind
the scenes. When developing a new
theme, no need for fancy redirects
or “under construction” messages.
Simply install the plugin and then
use it to view your new theme for
development. Very useful.
There are many examples like these, where basic functionality involving category
icons, random images, and popular posts may be implemented with a few lines of
code. You may have to spend a few minutes searching for (or writing) that perfect
script, but doing so will save you from having to install and maintain yet another
unnecessary plugin.
Of course, the idea here is to keep the number of plugins to a minimum. By doing
so, you eliminate extraneous script processing and help to ensure optimal site
Useful Plugins for Theme Developers
Here are three plugins that we have found to be virtually indispensable for theme development:
135
performance. Minimizing the number of plugins used for your site also improves
the likelihood of smooth upgrades. The fewer third-party plugins you have
running, the less opportunity there is for something to go wrong while upgrading
to the latest version of WordPress. Likewise with plugin updates: the chances of
conflicts decrease with the number of plugins installed. It’s all about facilitating
upgrades, avoiding conflicts, and fostering maintainability.
On the other hand, there are many situations where plugins provide the perfect
solution. Here are some great examples of situations where installing a plugin is
the best solution:
• Complex scripting needs or when a suitable alternative is not readily available
• Functionality that affects the core functionality of WordPress, such as caching
• Extensive functional enhancement, such as database, sitemap, or
spam management
• Functionality that requires signicant conguration of options and settings
• Enhancing administrative functionality with modied or additional admin areas
• You are uncomfortable editing code and would prefer doing things
the easy way
When it comes to these types of scenarios, choosing a plugin is most likely your
best bet. If so, do your research, find the best plugin for your needs, and give it
a go. As mentioned previously, there are thousands of free, easy-to-use plugins
available for immediate use. Regardless of your goals, there should be a plugin
that suits your needs perfectly.
136
5.1.5 Choosing the Perfect Plugin
The key to finding the perfect plugin is research. Taking the time to investigate
potential plugins ensures smooth implementation and prevents unnecessary
headaches in the future. Of course, many of the more popular plugins (such as
Akismet, XML Sitemaps, Database Manager) may be on your list before you even
install WordPress, but even so, you may be surprised at the growing number of
alternatives. Here are some tips to help you choose the perfect plugin:
1. Determine the need. What do you want the plugin to do for you?
2. Check the WordPress Codex. See if there are any suitable matches at the
official plugin repository. It is generally well-documented, well-organized, and
up-to-date.
3. Regardless of what you nd at the Codex, search the Web for alternatives.
Many developers deliberately choose not to list their plugins at the Codex.
There are some great plugins that are only available directly from the author.
4. Check the compatibility of the plugin before installation. Make sure that it
works with your version of WordPress.
5. Check the support of the plugin. Is there a way of getting help if you need
it? Is there a forum? Does the author appear to be responsive?
6. Determine how frequently the plugin is updated. This may provide
clues as to the level of commitment that may be expected from the plugin
author. There is nothing worse than relying on a plugin that fails to evolve with
new versions of WordPress.
7. Is the plugin well-documented? Does the documentation explain
everything adequately? Is there a change log or history of changes for the
plugin?
8. Search the Web for specic issues related to the plugin. Dig for the stuff
that isn’t mentioned on the plugin page. Search for phrases such as “problem
with name-of-plugin,” “name-of-plugin issues,” or even “name-of-plugin
sucks.” Also research the performance of the plugin so it doesn’t slow
you down.