Archives

Hackety-Hack, Don’t Come Back

Well, this has been a first for me. I was recently hacked on this very blog; or at least it was brought to my attention recently. I suppose it could have taken place a while ago. My hosting provider started sending me notices that my webspace account was generating large amounts of spam mail while I was away on vacation. So naturally by the time I got back home, they had shut down all of my websites. Of course, that was a good thing because they actually researched and found the offending scripts inside a WordPress theme that I had installed. Thus the reason that my blog is somewhat out of sorts now.

So a big thanks goes out to 1&1 Web Hosting for finding the problem and dealing with it in a reasonably quick fashion. Naturally I’ll be scrutinizing my WordPress themes a bit more closely from now on.

Upgrade Time : WordPress 2.9 and MySQL 5

WordPress 2.9 was release somewhat recently and so like a good WP minion I logged into my site and clicked the update button. Error… uh oh. Apparently WordPress 2.9 requires a more recent version of MySQL (>= 4.1.2). My host installed MySQL 5 ages ago but I’ve never bothered with migrating to the new version because I didn’t have a reason to. Now I do. So here’s how my upgrade went.

First I made a full backup of both the web content and the MySQL database. The MySQL database backup was just a mysqldump command like so:

Continue Reading

A WordPress Plugin – Hello XML-RPC World

In a previous post (How Can I Modify The WordPress XML-RPC System?) I outlined how one might create a plugin that adds functionality to the WordPress XML-RPC server. I’ve had some questions from some readers arise so I wanted to take the opportunity to provide a more concrete and usable example.

WordPress Plugin

First you are going to create your plugin. This can be a single file that follows all of the regular WordPress plugin flow. I have a complete plugin example listed below.

Of course, for simplicity sake in this post I’ve eliminated comment blocks. You’ll need those in order for your plugin to be properly recognized by WordPress. Other than that this is it. The wpHelloXMLRPCWorld method is the actual method that does the work of whatever you want. This one simply returns a hello world string. The attach_new_xmlrpc method is used to do the work of registering your new XML-RPC method. And finally we just need to add the action to tell WordPress to use our attach_new_xmlrpc method to attach our new XML-RPC function.

Testing With Java

I personally like the Redstone XML-RPC library, which I’ve written on before (Would You Like To Post To WordPress From Java?). This is a very simple Java class which can test the new XML-RPC function.

Download

This is a complete example that includes:

  1. The PHP file needed for the plugin. Just upload wpHelloXMLRPC.php to your wp-content/plugins directory and activate as usual for WordPress plugins.
  2. The Java program which can run your new XML-RPC method.
  3. A bat file (dos/Windows) that will compile the Java test program.
  4. A jar file containing the required Redstone libraries

wpHelloXMLRPC.zip

How To Manually Merge WordPress Tags

With the advent of “tags” in the WordPress blogging platform there have been several very good plugins introduced which can handle management of your tags. This includes merging tags.

Why Would I Want To Merge Tags?

One word… duplicates. In my case I created a tag for my Friday Song Of The Day posts. The original post slug was /friday-sotd/ however as time went on something happened and I noticed that somehow I had created a second tag with the same title but with /friday-song-of-the-day/ as the slug. Well, that’s a problem if I want to link to all of the posts in that tag, because there are really two different tags.

Ok, so you’d figure I would go out and get one of these shiny new tag plugins and be done with it in a few minutes, right? Heck no! I’m an under-the-hood kind of guy. I like to know how and why stuff works. So I set out to discover the changes that would be needed in my database to perform the merge. Why? Well, now I have the capability to write my own plugin if I so desire… or I could write a blog post to teach other people how to do it. πŸ™‚

So How Do I Do It?

Well, as it turns out this is really quite simple. There are just a few queries that you’ll need to run using the command line MySQL client or a graphical client such as phpMyAdmin.

Step 1) Backup Your Database!
This should go without saying… but I’ll say it. Before making any major modifications to any live system you should make a backup. I wrote an article on creating a Bash Script To Backup Your Website which you can use or one of any other numerous methods.

Step 2) Get The Tag IDs
Next you are going to need to know the IDs of the tags which you are going to be merging. The “tags” are stored in the wp_terms table. In my case I performed a SQL search in the table such as this:

As you can see in the following screenshot of the query results, the two IDs that I am interested in merging are 47 and 91. I note this and move to the next step.

Step 3) What Posts Are Affected?
This step is not really required. You could simply head off to the UPDATE step and trust that everything will be fine. I myself like to know what I will be affecting though, so this query will return some information about all of the posts that are tagged with our two tags:

Here is a screenshot of the results:

Yeah, that gives us somewhat of a picture but there are just too many results to see all at once. Let’s refine that query a bit to just give us a count of the posts related to each tag. Now the query has changed to:

And here is the screenshot of those results. As you can see now I have a nice count. 116 records are tagged with the /friday-song-of-the-day/ slug and 25 are tagged with the /friday-sotd/ slug.

Step 4) Merging Tags… Commence!
Well, you may be disappointed, but the query to perform the merge is actually a simple UPDATE query. Here it is:

Simple, right? All we are doing is changing the listings in the relationships table that are ID 47 to 91. So now there should be no posts that are tagged with the “tag ID” 47. Below is a screenshot after running the query and you can see that 25 rows were affected. This is the same number that you can see above when we counted the posts under each tag.

Now if we run our query to count the number of posts under each tag we get 141, which is the sum of the original two counts 116 + 25. Great!

Step 5) Cleanup
Well, we could stop here, but there are still a few leftover artifacts from the old tag (ID 47). Let’s go ahead and get rid of them:

Step 6) Redirect
What! We’re not done yet? Almost. After making the above changes I went to double check my work by visiting the actual blog. I pulled up the tag archive page for the old tag by going to http://www.franzone.com/tag/friday-sotd/. It works! Now I’m presented with a “No Posts Found” page and a search box. But… that’s not very appealing. I believe I’ve actually linked to this URL in past posts, which could be a problem.

htaccess file to the rescue! This is actually a pretty simple fix. I just opened up my .htaccess file in a text editor and added a file like this:

Now if you visit that URL you get redirected to http://www.franzone.com/tag/friday-song-of-the-day/ instead. Perfect!

Final Notes

So there you have it. You can go about merging your own tags and hacking up your WordPress database. Of course you could take a scripted approach to the above steps. Or you could create your own tag merging WordPress Plugin. Or you could leave a comment below if you were so inclined! Thanks for stopping by.

Inserting A Picture Into Your WordPress RSS 2.0 Feed

I use NetVibes for my browser home page and RSS feed reader and I’ve noticed over time that some of the feeds that I subscribe to have pictures embedded within their feed summary. Why doesn’t my feed have a picture in it? I thought that this was really neat because it gives you a quick visual queue as to what the article may be about. It is also eye-catching so that among 5 or so feeds on a page you are instantly drawn to the one with pictures. So how can you get a picture into your feed?

RSS 2.0 Specification

The RSS 2.0 specification has an <enclosure> element available at the <item> level. This element can specify various types of media that you want attached to your feed item. In our case we want that to be an image. The element should look something like this:

<enclosure url=”[URL To Media File]” length=”[Size Of Media File]” type=”[Mime Type Of Media File]” />

All three attributes are required:

  1. url – This tells the feed reader where the media file is located
  2. length – This is how big the media file is in bytes
  3. type – This is the standard MIME type of the file

WordPress Action Hooks

So, how do I get this information into my RSS 2.0 feed? Luckily the good folks at WordPress have included a little action hook in the file that generates your feed. So all we have to do is write a function and hook the action! For my purposes I was writing a new theme so I just included all of this in my functions.php file. You could modify the functions.php file of your existing theme or this could actually be made into a plugin.

First we have the function that does the heavy lifting. The first thing I do in my function is get the image URL. For my purposes I store the image URL in a custom field attached to each post. I also include a default image inside my theme in case I forget to supply an image for a post.

Next I calculate the MIME type of the file. Now there are some really cool functions out there that actually look inside the file to get to the real MIME type. I don’t really need to be that sophisticated since I’m in control of the image so I’ll just use the file extension. I’m supporting PNG, GIF and JPEG (default) image formats.

The final piece of information we need is the filesize. That is a quick and simple function call if you have the absolute path to the file. I found a good function to get that information on the PHP site.

Now just echo out the enclosure element.

The action hook looks like this and should come outside of the function you define. The first argument is the action hook in WordPress. The second argument is the name of our function.

Putting it all together we have.

Conclusion

This article describes part of the reason I love WordPress so much. They were so thoughtful to leave so many useful and meaningful hooks into the platform and that makes my job as a web developer that much easier. Oh and by the way, the above trick won’t work on every RSS reader. That is implementation specific as to whether the reader decides to display the image or not. All you can do is server it up to them.

Happy WordPress 2.5 Day!

WordPressWell, anyone who is “in the know” knows that the 2.5 version of WordPress has been out for a while. Ok, but a guy can’t just go updating his blog all willy-nilly. There are plugins that must be checked for compatibility and data to backup and so forth and so on. Yes, now I’m just rambling, but it is really late and I am tired.

So anyway, I absolutely love WordPress 2.5. The new administration interface takes me to a happy place. πŸ™‚ I actually didn’t have too many problems upgrading either. One of my plugins stopped working, but oh well. I already have a decent replacement… I just need to work on a little placement / CSS issues to get it to look like I want it to. Well, I sincerely hope you have a good and productive week! (That was very positive; I don’t usually come off that chipper).

Looping Through Pages In WordPress

In a recent article I outlined how to modify the output of the wp_list_pages() template function in WordPress. Later on, however, I found that having the <span> tags that I added in every single title was not desirable. So instead of using a filter I wrote a small bit of custom code to iterate through my pages instead of using wp_list_pages(). Here is how to do it.

Wherever you need to output your pages just use some code like this:

Writing A WordPress Filter To Modify Post/Page Titles

I was recently working on a new WordPress theme when I had need to modify the title of pages before output. The style I was attempting to use involved wrapping <span> tags around the title (inside of the href tags). The wp_list_pages() function returns pages (which are basically the same as posts) and has several arguments that you can pass to customize it’s output. However, modifying the title output is not one of the arguments.

Filters to the rescue! I simply opened up the functions.php file and added a function like this:

Pretty simple, eh? All that is left to do is to register this function as an appropriate filter, which you can do like this:

The first parameter tell WordPress which function/activity you want to “hook” into. The second parameter is the name of your function. The final parameter is a priority specifier. The lower this priority number the earlier it will execute.

Done! Now instead of output like this:

I get this:

For extra credit you actually throw all of this into a plugin. That way you wouldn’t necessarily have to modify any of your theme files.

Where Did My Categories Go?

Organizing Your BlogTo tag heaven. Ah, yes… tags. The teacher’s pet of Web 2.0. The category list on this blog had become rather unwieldy as I just plugged in new categories whenever I did not feel like putting any thought into organization. I never really liked that method and so when WordPress implemented tags I figured I’d make the move. The only problem was converting all of my old categories into tags. This was quite an undertaking and it actually took some MySQL DB tweaking… muahahaha!

Converting Categories To Tags

Fear not, though! There is a Category to Tag converter in the WordPress admin under Imports. This came in quite handy. However, if you did like me and waited too long you may have some terms that are both a category and a term. For instance I used Apple as a category for a while and then started tagging posts with Apple instead. The Category To Tag converter will not convert your category in this instance… bummer. If you need to solve this problem go back and remove all of the Apple tags from posts and mark them with the equivalent category. Once that tag is completely gone from all posts you can delete the tag in your system. How do you do that?

Well, using phpMyAdmin or another similar SQL tool for accessing your MySQL database issue the following query:

This will get the term_id for you which you will need in the next query. So write down the term_id and move to the next step:

OK, if you truly do have both a category and a tag in your system there should be two entries showing. One will have a taxonomy = ‘post_tag’ and the other will have a taxonomy = ‘category’. In phpMyAdmin you can just click the red X next to the ‘post_tag’ row and DELETE it. Otherwise you can issue a query like this:

Now that the tag is completely gone the converter should allow you to convert this category to a tag… and then your laughing (as the British say).

How Can I Modify The WordPress XML-RPC System?

One of the great things about WordPress plugin development is the abundance of open source content to learn from. Ever see a plugin in action and wonder, “how does that work?” or “How can I modify the WordPress XML-RPC system?” Well, all you have to do is download it and see! Most plugins are licensed under the GPL license (General Public License).

One of my recent projects involves working with the WordPress XML-RPC interface. The problem is I needed the XML-RPC interface to do more than the current implementation offered. Initially I wrote a patch for the xmlrpc.php file to accomplish what I needed. It never actually occurred to me that the smart folks at WordPress would actually give you a way to write hooks into the XML-RPC interface to add more functionality. Sweet! I came to this revelation thanks to a plugin called wpLinkMentor, a plugin that allows you to manage your blog links over XML-RPC.

Logging

An important part of most applications is logging, right? This provides an insight into what is going on with your program/plugin and can be extremely helpful in debugging problems you might encounter. The following snippet allows your plugin to make log entries into the standard WordPress XML-RPC log.

Error Handling

The way to handle errors in your XML-RPC plugin is to throw an IXR_Error which is somewhat like an exception in other languages.

Making The Magic Hook!

Ok, all of that is fine and dandy, but how do I hook into the XML-RPC system? Where’s the beef!? Here it is in all of it’s simplistic glory, just like most of the WordPress architecture.

I could not have imagined it being so simple. Just write your methods to do whatever you desire and them register them as in the previous code sample. Have you written any WordPress plugins that utilize the XML-RPC interface?