Optimizing the performance of WordPress

It’s no secret that I LOVE WordPress. I’ve been using the software for 6 years and have no plans to stop any time soon. Last year I attended WordCamp LA with @JasonKeller, it was awesome. I knew that I had to be involved with WordCamp 2010.

When WordCamp 2010 was announced, I signed up as a speaker. My topic, “Improving the performance of WordPress”.

I had a great time preparing and delivering my presentation. A lot of people asked me to post the slides online. I decided to do one better, and post a recorded version of my presentation.

Download the slides in PDF format

WordPress asking for FTP username/password?

Are you annoyed with your wordpress install asking you for your FTP cradentials everytime you try to upgrade a plugin? I know I was.  Thankfully, there’s a simple solution which will save the FTP login (username/password/server) so WordPress stops asking you for them.  Here’s how you do it.

  1. Locate your WordPress root diectory and find the “wp-config.php” file.
  2. Edit the file and insert this somewhere in the middle as it’s own block, we normally put it after the mysql username/password block.
define("FTP_HOST", "localhost");
define("FTP_USER", "yourftpusername");
define("FTP_PASS", "yourftppassword");

Once you’ve made that edit, save the file and voila! No more punching in your ftp username and password all the time.

timthumb.php returns a 404 Error in WordPress

timthumb is a great little php script that helps create thumbnail images (http://code.google.com/p/timthumb). I’ve used it before, but recently I purchased a wordpress theme that used it, and it wasn’t working.

I would get a 404 error when I tired to access the directly via a url like this:
theDomain.com/wp-content/themes/theTheme/scripts/timthumb.php?src=/wp-content/uploads/2010/05/moose1.jpg&h=207&w=237&zc=1&q=80

After some digging around in log files on my server I realized the error

SoftException in Application.cpp:610: Directory “/home/usernamehere/public_html/wp-content” is writable by others

My problem had to so with the directory permissions! “writable by others” means that the permission on the directory were set to “777”. I changed the permissions of the directory using chmod to 755, and everything started to work.

Lesson learned, if timthumb.php is not working at giving you 404 errors, check the permission of the directory that contains the php file.

Create WordPress Thumbnails Automatically

create wordpress thumbnails automaticallyWordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as meta-data. This meta-data can be used by theme authors to do some pretty cool things.

The theme I am currently using allows for a custom field named “thumb”. If you create the “thumb” custom field in a post and pass in the path to an image, for example “wp-content/uploads/2008/12/wordpress_logo.png”, a thumb nail version of the image will appear next the posts title on the home page of my blog. Awesome stuff.

When I discovered this theme and the “thumb” custom field, I already had 225 posts in my blog. I didn’t want to go back and manually create all the custom fields for the existing posts, so I wrote a PHP script to do it for me.

The PHP script cycles through your existing posts and looks for the first image in the post, and creates the thumb field based on it’s location. The script is configurable, your theme may want a different custom field name, like “thumbnail” or “image”.

This code is provided as is. If you aren’t a developer, you should be messing with stuff like this. Be sure to back up your wordpress database before running this.

Instruction:

  • download this code
  • configure the database connection info and custom field name
  • upload this code onto your server
  • point your browser to the file
  • watch the script do it’s magic
  • remove the file from your server
  • donate $2 to my paypal account (joshhighland@gmail.com)

Note: This is a one time deal, when you create new posts you will need to add the “thumb” field and image path manually

<?
 
//your database configuration information
$username = "YOUR_USER_NAME";
$password = "YOUR_PASSWORD";
$database = "DATABASE";
$databaseLocation = "localhost";
 
//name of the Custom Field to use
$customFieldName = "thumb";
 
/******************* END CONFIGURATION *****************************/
 
//get the wordpress posts to work with
$query = "SELECT * FROM wp_posts";
 
if ($result = $mysqli->query("$query"))
{
	//loop over all the posts
	while ($row = $result->fetch_object())
	{
		//get all the info from the post that we need to work with
		$cur_post_id = $row->ID;
		$cur_post_content = $row->post_content;
 
		//the pattern to find the html image tag
		$pattern = '/]+src[\\s=\'"]';
		$pattern .= '+([^"\'>\\s]+)/is';
 
		//try to match the pattern against the post content
		if(preg_match($pattern,$cur_post_content,$match))
		{
			//break up the image path
			$pieces = explode("wp-content/", $match[1]);
 
			//make sure the file name isnt empty
			if($pieces[1] != "")
			{
				//build the file path
				$imagePath = "wp-content/" . "$pieces[1]";
 
				//insert the custom field for the post
				$insertQuery = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES ($cur_post_id, '$customFieldName', '$imagePath')";
 
				$mysqli->query("$insertQuery");
 
				//debugging stuff
 
				if ($mysqli->errno)
				{
					printf("failed: %s \n", $mysqli->error);
				}
				else
				{
					echo "..... inserting $imagePath \n";
				}
				flush();
			}
		}
	}
}
 
echo "---completed---";
 
?>

Manually reset your WordPress password

lost-wordpress-password

It doesn’t matter what application your working with, losing your password is always a pain in the ass. Luckly if you are working with wordpress (man, I blog a lot about wordpress these days), on your server and you have access to the MySql database, resetting your password manually is a snap.

  • Login to your PhpMyAdmin
  • Select your WordPress database and click on the “SQL” button to open the SQL query window.

wordpress-phpmyadmin

  • Paste the following code in the window textarea. (Don’t forget to modify the password and username before executing it)

UPDATE ‘wp_users’ SET ‘user_pass’ = MD5(‘PASSWORD’) WHERE ‘user_login’ =’admin’;

That’s it! Your password has been reset, and you should be able to login to your wordpress admin area once again.