src="http://c2103932.cdn.cloudfiles.rackspacecloud.com/wp-content/uploads/2007/04/wordpress-logo.gif" alt="wordpress logo A great little trick for debugging WordPress problems" title="Wordpress logo" width="120" height="120" class="alignright size-full wp-image-279" />This week I upgraded a new client’s WordPress blog from version 2.6 to 2.9.2. All went just dandy.
Then today he emailed me to say that he’d noticed his post excerpts were not appearing where they were supposed to, in single post pages. On closer inspection I noticed that the excerpts displayed fine on the homepage, but — despite the same exact code being used — they weren’t showing up on single post pages. In both cases the the_excerpt() function was being used without parameters. Weird.
How would you go about debugging this one?
My immediate thought was to blame a plugin. Why? Because WordPress’s great coding strength is also its weakness —
href="http://www.raymondselda.com/understanding-action-hooks-in-wordpress/">hooks and
href="http://www.raymondselda.com/understanding-filter-hooks-in-wordpress/">filters give plugin authors the ability to mess with the output of “core” WordPress functions. But it’s really hard to tell if any messing around is going on, in order to debug problems.
Question then: how do you figure out if one or more plugins is using a hook/filter to mess with the output of a given WordPress function?
If you have shell access to your WordPress host I’ve got a little treasure for you today. A simple enough shell command that will make your life a lot easier. (If you’re a “real” programmer you’ll almost certainly know about this already, but if you’re not, you’re gonna love it!)
Now, shell access (SSH) gives you “command-line” control of the operating system on which your WordPress blog resides. If your hosting provider has given that to you and you know how to use it, you can do cool stuff like ZIP up all the content in one hit so you can back it up really easily, say. Lots of fun to be had.
Today I want to show you how to return search results for all the instances where a certain string appears in your plugins’ PHP files. That’s how we’re going to debug my problem with the the_excerpt() function today.
Here’s the command to run at your SSH prompt when you’ve navigated into your wp-content/plugins directory:
find . -name \*.php -exec grep mystringhere {} +
(Note that on older systems that last plus sign might need to be replaced with a semicolon “;”.)
That command simply isolates all php files and searches through each in turn for each occurrence of “mystringhere”. For each occurrence it finds, it shows you that line of code preceded by the filename and line number. Easy!
Because I don’t like the black and white of my Terminal application, I then copy out all the results found into my code editor, so that I get to see the code in all the pretty colours that
href="http://panic.com/coda/">Coda puts it in. That’s up to you.
For purposes of example,
href="http://www.alistercameron.com/download/find_grep_example.html">here are the results I got from my client’s plugin directory (pops up a new window).
Yes it looks like a huge mess, but you can see now that it’s really easy for me, in my code editor, to search through all that for “add_filter” and “add_action”, to see where any plugin is messing with the output of the the_excerpt() function.
What all that search results mess (see link) tells me is that there are seven plugins that may be the culprit. In my case, I was fairly sure the Organize Series plugin wasn’t supposed to be active, and when I deactivated it… HEY PRESTO… problem solved. The excerpt came back!
If your hosting provider doesn’t give you SSH (or you’re not prepared to pay extra to get it), all is not lost… if you’re a Mac person. Download a backup of your site to your local machine, and then run the same command locally (from the plugins directory), using Terminal.
Comments welcome on what may be a good Windows equivalent approach.
class="entry-social-links">
href="http://www.retaggr.com/AddMe/alicam" target="_blank">
![]()
src="http://content.retaggr.com/static/addme.gif" alt="addme A great little trick for debugging WordPress problems" title="Click to see all my profiles on the web..." />