Tải bản đầy đủ (.pdf) (10 trang)

Tìm Hiểu về Wordpress - part 39 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.48 MB, 10 trang )

367
• Combine CSS and JavaScript - There are many great resources available on
the Web for consolidating, combining, and compressing your CSS and JavaScript
files. WordPress users should check out WP Minify which
uses the Minify PHP-5 application used to combine multiple
CSS and JavaScript files, compress their contents, and serve the results with HTTP
encoding (gzip or deflate) and headers that allow optimal client-side caching.
• Progressive image-loading - Instead of loading all of your images at once,
delay loading of images in long web pages until the user scrolls down to the
next section of the web page. This is opposite of preloading all of your images
at once. With progressive image-loading, users will experience a slight delay
in the loading of images as they scroll down the page, but for pages with lots
of images, progressive loading can reduce the number of simultaneous HTTP
requests and greatly improve performance for the initial page load. Check
out the jQuery plugin, Lazy Load to easily implement
progressive image loading on your site.
• Use a content-distribution network - A Content Delivery Network (CDN)
is a network of computers cooperating to deliver content for web sites. Using
additional servers to deliver images, downloadables, and other static files
reduces the load of your primary server while improving the performance and
scalability of the overall website. Some commercially available CDNs include:
• Akamai Technologies - /> • Amazon CloudFront - /> • BitGravity - /> • CacheFly - /> • Internap - /> • Limelight Networks - /> • Mirror Image Internet - /> • SteadyOfoad - />368
While working to reduce the overall number of HTTP requests for your site, there
are several ways to check how many requests are being made from your pages. By
simply opening a text editor and scanning your CSS file for all instances of the
“url()” value, you can determine how many images are being called just for your
design.
In your browser, you can also check the source code of your pages and examine the
number of files linked to in the <head> area. By searching your source code for all
instances of “<img”, you can see how many regular images are being called.
If you are using Firefox, the awesome and absolutely essential Firebug extension


can be used with Yahoo’s helpful YSlow add-on
to analyze your site’s performance in real time, with nice
graphical representations showing many different aspects of your site, including
the overall number and duration of HTTP requests.
Here is a screenshot showing how Firebug together with YSlow can help you
analyze the number of HTTP requests made by any page:
369
9.5.6 Plugin Maintenance
Many articles on the Web will advise you to disable unused plugins as a way of
optimizing your site. While this is partially true, there is much more to the story. It
all begins when deciding which plugins to install. Do some research and make an
informed decision before implementing each of your plugins. Every plugin that
you install requires resources and may decrease performance. Plugins also affect
performance in a cumulative fashion. Even so, I have seen sites running 50 or more
plugins that load very quickly. Just keep in mind that, unless you know what you
are doing, loading up on unnecessary plugins can really slow things down.
Next, once you begin installing plugins, remember to test as each one is installed.
Don’t just slap it in there and walk away – take the time to surf your site and
examine its performance with the newly installed plugin(s) installed. If you find
that they are slowing things down, reconsider your choice and search for better
options. Otherwise, do what you can to offset the reduced performance, perhaps
by caching (see Section 9.5.2 in this chapter).
Optimizing your database is a great way to improve site
performance. There are numerous ways to do so, including the
following:
• Manual SQL query - using the “OPTIMIZE TABLE” command,
it is possible to optimize any specific table or group of
tables. Use this format, replacing the example terms with
your table names:
OPTIMIZE TABLE `feeds`, `items`, `options`, `tags`

• Using phpMyAdmin - check all tables in your database and
select “Optimize” from the dropdown menu.
• Use a plugin - Joost De Valk’s Optimize DB plugin makes it
easy to optimize your database: />Optimize Your WordPress Database
• Use a different plugin - Another good plugin for
optimizing and cleaning up your database is WP-Optimize
Also check out WordPress
Database Table Optimizer to
automate the optimization process.
• Use a completely different plugin - The WP DBManager
plugin also makes it easy to
optimize your database, along with a whole bunch of
other great database-management features including
automatic database backup and more.
Regardless of your method of choice, it is good practice to
optimize your database on a periodic basis to help ensure
optimal site performance.
370
Then, as you continue to take control of your plugin arsenal, keep an eye on any
that may have been useful at one time but now are useless. Uninstall any obsolete
and unused plugins immediately. Every time you load a page on your site, all of
your plugins are loaded in the background. Thus, any plugins that you can remove
from this equation will provide an immediate performance boost.
Also, as mentioned above, keep your plugins updated. Whenever you see that
a new version of the plugin is available, take the time to check it out and then
upgrade as soon as possible. Staying current with plugins is a good idea because
they usually feature improved code, tighter security, and better features.
Finally, once you decide to disable a plugin, remove it completely. Don’t leave it
sitting there in a disabled state in your Admin area. Uninstall it, and make sure that
any peripheral files that may have been related to the plugin are also removed.

Hopefully, any plugin that you decide to install will provide a complete “Uninstall”
option that cleans up after itself in the WordPress database, but if not, you may
want to go in and manually remove all traces of it.
9.5.7 Database Maintenance
If your theme is the heart of WordPress, then your database is its brain. The
database contains all of the settings, comments, posts, plugin options, and
administrative data required for your site to function properly. When this data
becomes corrupted or is poorly optimized, performance may suffer. A good
practice to get into involves optimizing and backing up your database at regular
intervals, depending on the use and scope of your site. Highly active sites should
optimize their database every day, while less-active sites may be fine optimizing
once a week or less.
You should also take every opportunity to clean up your database of unnecessary
data. For example, after installing, testing, configuring, and uninstalling plugins,
you should examine your database for any tables or options that may no longer be
necessary. If the plugin developer did their job, their plugin will include a complete
uninstall option that cleans up the database for you. Unfortunately, this is not
always the case.
Too Many Plugins?
As this book goes to press, we
are currently running a poll at
the Digging into WordPress
companion site that asks,
“How Many Plugins do You
Use?” Drop by and vote if the
poll is still open when you read
this, otherwise check in to see
the results:
/>371
9.5.8 Other Optimization Techniques

There are many ways to optimize your site and improve performance for your
visitors. Here are a few more ideas that are well-worth the effort.
Split long posts into multiple pages
WordPress enables you to split up long posts into individual pages using the
<! nextpage > tag in your posts. This trick is not for all posts, but for those super-
long articles with lots of images and stuff, it may really help to speed things up.
Limit the number of posts on archive and index pages
The idea here is to reduce the overall size of your web pages by displaying fewer
posts. Instead of, say, 20 posts on your homepage, you can use a plugin such
as Custom Query String Reloaded to limit the number of
posts that are displayed. By doing similar for other types of page views, you can
customize page size in granular fashion.
Specify far-future expires or cache-control headers
For static resources such as images, CSS and JavaScript files, a great way to decrease
load time for your regular visitors is to specify far-future “Expire Headers.” By
setting an expiration date of, say, a month for your image files, visitors’ browsers
will know not to re-fetch them, thereby reducing their load-time significantly.
Here is an example showing an HTAccess technique used for three common types
of images:
# EXPIRATION HEADERS FOR IMAGES
# note: 2592000 seconds = 1 month
ExpiresActive On
Cache Your Database
Improve database performance
by caching your database
queries. Check out the DB
Cache plugin to cache your
queries and speed up the
loading of your site.
/>372

ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/tif A2592000
ExpiresByType image/ico A2592000
Just keep in mind that with cache-control headers in place, you will need to change
the file name of any files that are being cached in order for them to be refreshed
in the user’s browser.
Reduce image sizes
Obviously, large images require more time to load than smaller ones. There are
millions of articles out there explaining how to optimize your images, so we
won’t reinvent the wheel here. Instead, let us point out several avenues of image
optimization that will help you determine the best approach:
• Online image optimization tools - There are many great online services
such as Smush.it or punypng that
make it easy to upload and optimize your image sizes. Best of all, these free
online services work on JPEG, GIF, and PNG images. Both services use “lossless”
compression techniques to ensure that your images do not lose any of their
original visual quality.
• Ofine optimization programs - For more control over the details of
your image optimization, check out OptiPNG or
PNGcrush Either of these powerful command-line
programs will remove unnecessary data from your PNG images without
reducing image quality. Both are excellent programs that work beautifully.
• Optimization during image creation - Arguably the best way to optimize
your images is to create them optimally to begin with. There are many ways
to do this, and there are plenty of excellent articles that will help you use
programs such as Photoshop to take advantage of its image-optimization tools.
373
Hardcode database calls to improve performance

Speed things up by eliminating unnecessary calls to the database. A classic
example of this is seen in the header.php file of most WordPress themes. Here is a
typical example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" /><html xmlns=" <?php language_attributes(); ?>>
<head prole=" /> <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>;
charset=<?php bloginfo('charset'); ?>" />
<title><?php wp_title('&laquo;', true, 'right'); ?><?php bloginfo('name'); ?></title>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
If you are releasing your theme for public consumption, the database calls made
by these template tags make it easy to display the correct information on any
WordPress-powered site. For themes that do not require the convenience that
these template tags provide, however, it is possible to reduce the number of calls
made by replacing the tags with their plain-text values.
If you go through your theme files, you will see there are many of these dynamic
calls that may be replaced with static values. To help illustrate, here’s what
the previous code would look like after replacing the dynamic tags with their
corresponding, site-specific, plain-text values:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
" /><html xmlns=" lang="en-US">
<head prole=" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Digging into WordPress</title>
374
<link href="
rel="stylesheet" type="text/css" media="screen" />
<link rel="pingback" href=" />
Disable unused/unnecessary plugins
Plugins consume resources. Running plugins you don’t need is stupid. Go through
your list and uninstall/delete any that you no longer need. Trust us, you’ll be glad

you did.
To Infinity, and Beyond
As we have seen in this chapter, there will always be a way to improve the
performance of your WordPress-powered website. Out-of-the-box, it’s going to
work just fine for just about anything, but that is no reason to get complacent.
Even with the smallest amount of effort, it is possible to improve your existing
functionality and further optimize your current setup. From simply installing
a few caching plugins to manually implementing custom functions, your site’s
maintainability, security, and performance can be advanced as far as you are
willing to go.
375
Screenshot of WordPress-
founder Matt Mullenweg’s
personal website.
Thanks Matt.

376
In the beginner‘s mind there are many
possibilities, but in the expert‘s there
are few.
– SHUNRYU SUZUKI

×