Archive for the 'wordpress' Category

Downtime on Thursday Afternoon

Friday, June 13th, 2008

Apparently some of you had the doubtful pleasure of seeing my directory index between 4 and 5:30 pm yesterday. Apparently I set their server on fire by (check this out) flushing the WP-Cache to see the small tweaks in the css style sheet. I made a small change, flushed the chache, refreshed the page, fixed it, flushed, refreshed and etc. So for like 20 minutes the site was mostly un-cached. Then the shit hit the fan and I got locked out of my own fucking blog only to find this in my mailbox:

Hello Lukasz,

I apologize for the interruption, but I have had to temporarily disable your site. Unfortunately, this was causing a huge load on the machine:

popcorn: 01:21 PM# uptime
13:21:58 up 78 days, 21:26, 8 users, load average: 96.58, 73.29, 53.76

After disabling this, the load dropped significantly:

popcorn: 01:22 PM# uptime
13:25:32 up 78 days, 21:30, 8 users, load average: 29.25, 51.65, 49.33

You probably need to enabling caching for this blog or disable any extraneous plugins before re-enabling it. You can check out more about it here: http://wiki.dreamhost.com/WordPress_Troubleshooting

Thanks!

I know that you get what you pay for, but I was not really happy about them just rendering my website inaccessible without:

  1. Letting me know there is a problem, and allowing me to fix it
  2. Telling me how to restore my website to usable condition again

Fortunately all they did was to rename index.php to disabled_index.php so I was able to quickly get it back online. I also tried to comply with their whiny demands and made Caching much more aggressive. Let me know if you notice any weirdness on the page - especially stuff like CAPTCHA’s not working properly.

Here is my reply to them - note that I was a tad pissed off when I wrote this. In retrospect perhaps I was a bit harsh on them. What do you think:

First, how to I enable my site back? I’m getting emails, my readers are not happy and I’m losing advertising revenue. It would be nice if I got some instructions on how to do it. I’m guessing I just rename disabled_index.php back to index.php? Thanks for telling me that though.

Second, all I’m running on my account is a basic Wordpress account with couple of plugins - chiefly WP-Cache, Akismet, and Bad Behaviour which aim to reduce the system load, and prevent spam. Two out of these were specifically listed on the troubleshooting site you linked to in your email. There is really not much else there.

You want to know what I was doing in the last few hours? I was tweaking my .css stylesheet and I cleared the cache few times to see the results. Could something as minor as that be the reason of such a huge spike? I will refrain from doing this in the future, but geez!

I haven’t added any plugins in the last 6 months. I haven’t changed the theme. I haven’t run any scripts. All I do on that account is blog. There is really nothing there that would be out of ordinary - just Wordpress installed via the one-click install from the panel. The only changes I really did to it in the past 6 months were small css tweaks.

Third, over the last year or so, I frequently witnessed the system load on popcorn to spike up way above 200. Some of my readers joke around that the site often takes up to 3 minutes to fully load during peak activity hours. Why is that? Is Wordpress really such a resource hog? Do I really get this many hits per second? I really doubt it. There is no way for me to control how much traffic do I get. I’m really not running some crazy scripts in there - it is just a fairly standard Wordpress install with a default theme. I can mess around with it a bit and try to switch off plugins but I really don’t think that will reduce the load of this website that much - I mean there is not much to disable there.

Really, I understand that it is hard to run a shared hosting company. I was always with you guys. I have been always very tolerant, and a loyal customer. I even recommended you to some of my friends despite the apparent flaws. I dealt with the regular downtimes, and I didn’t really say anything when you guys accidentally charged me over $200 for the next 3 years of hosting few months back. It happens. But disabling my website for undisclosed time period on a whim without giving me any prior notice and any possible chance to fix the issue before the website goes down is just pushing it. It’s unprofessional, and very disappointing. I’m hurt, I’m angry and I’m annoyed. Perhaps it is time for me to start looking for a new home.

If you see an activity spike on popcorn later today it might be because I’m backing up all my files and preparing to move to a new host.

So please tell me, what am I supposed to do here? What can you offer me? I’m not really interested in running a website composed from static HTML pages - I actually do want to use Wordpress and I do want to keep runing Akismet and other anti-spam plugins which keep it clean. I don’t really want to spend hours, hacking Wordpress and trying to figure out how to optimize the plugins that I use. I just don’t have time for that. And I definitely want more people visit my website - not less.

So what are my options? Will my blog get disabled every time there is a traffic spike? I mean, what if (God forbid) one of my posts ends up on a front page of Digg tomorrow? I’m not saying it will but it happened before, and I hope it will happen again. What then? Will popcorn spontaneously burst into flames?

Why should I stay with dreamhost?

I’m not a happy customer right now.

Was I out of line? I really think I will need to find a new host soon. I know I say it all the time, but stuff like this might just be the thing that will push me to finally do it. So this is once again time to pimp your host. Who do you use? What are their prices? What plans do they offer? How is their support like?

Update - it seems that I actually got a relatively knowledgeable and thoughtful response:

Hi Lukasz,

I’m the resident WordPress nerd around these parts and I can understand how upset you could be. Still, your site was causing undue strain on the server. I don’t think it has anythig to do with clearing out WP-Cache tho. I do notice that you are running Bad Behavior as an anti-spam plugin. Normally. that’s okay - but for whatever reason Bad Behavior and our system don’t seem to play nice with one another when a site is being hit by a spam attack. That very well could have been what was happening here - but as I wasn’t the one who disabled you, I can’t tell.

I run the WordPress blogs on our here (blog.dreamhost.com and dreamhoststatus.com) and I’d be happy to give you a few pointers to keep things lean and mean. First up, you’re doing the right thing by running WP-Cache. Unfortunately, the programmer isn’t actively updating that plugin anymore. The successor to that plugin is currently WP-Super-Cache. More info about it can be found here:

http://wordpress.org/extend/plugins/wp-super-cache/

As for spam fighting plugins, sticking with Akismet is good - but using WP-SpamFree in place of Bad Behavior is a bit kinder on CPU cycles. We use this as the one-two punch on our blog and the spam that Akismet has
to process has gone down considerably. Check it out when you have a moment:

http://wordpress.org/extend/plugins/wp-spamfree/

Turning that on and getting rid of Bad Behavior will actually take your overall memory usage down quite a bit, so please try that out and let me know how it goes.

Also, I’ve poked around your database and have noticed that the table for Top Post By Category has eaten up 111.6 MB of your database. That’s more than 3/4 of your total MySQL database size right there. If you are no
longer using that plugin, it might be a good idea to clear that category out. In fact, it’d probably be best to stop using it as that will help speed things up as far as page generation goes.

If you’re curious as to why you should stay with us, hopefully the observations I’ve offered you on how to make your site run a bit better in our environment will make a convincing argument. Most companies would leave the ball in your court when it comes to optimization - but I honestly want you to get all the traffic you can. Outside of the WP blogs here, I have my own sites (hosted here) and I know the feeling of folks outside of your immediate social circle checking out what you’re doing. It’s pretty nice.

Anyhow, try my suggestions out if you’d like and please go ahead and let me know if you have any further questions. I’ll be more than happy to help you out in whatever way possible if you do.

The nice thing about Dreamhost - one of their few redeeming factors is that their support staff is usually quick to respond and usually knowledgeable and helpful. Well, with the exception of that tool who renamed my index.php and didn’t tell me about it that is. My past interactions with them were very positive. Can’t hurt to try his suggestions for now. The top posts plugin is gonzo for now! I liked having it in the sidebar, but I did not realize how database intensive it was. Oh well, good riddance. I will try to switch over to wp-super-cache and wp-spamfree over the weekend which may or may not speed up the site a bit. We shall see.

Saving Bandwidth and Preventing Hotlinking With Coral Cache

Monday, August 20th, 2007

A while ago Jeff Artwood wrote about “outsourcing” hosting your images to a 3rd party service as a method for minimizing bandwidth costs. This a very practical advice not just for high traffic sites but for just about anyone. You never know when you might get on digg or slashdot and get a significant traffic spike. Jeff outlined various image hosting services in his post, and all of them have certain flaws:

  1. Image Shack and Photo Bucket have prohibitive storage and bandwidth caps
  2. Flickr has an upload cap and a restrictive TOS
  3. S3 is cheep but not free

There is an alternative to offsite image hosting though: Coral Cache.

Coral Cache Logo

The image above is a good example of Coral Cache-ing your images. It is both hosted on my server, and distributed by the Coral Cache service. How is it done? Click on that image and check out the URL. Every time you want to Coral Cache something simply add .nyud.net to the domain name. That’s all there is to it.

If you do it manually for all your larger images you get a fine grained control on which sections of your website are cached and which are not. On the other hand, this approach can become difficult to maintain. If one day you decided you no longer want to use Coral Cache you will have to search and change the URL for each image.

What you really want is to have normal URL’s for all the locally stored files, and then use mod_rewrite to automagically alter all of them to use Coral Cache. For example, if you are like me and you usually upload image files via the browser using Wordpress’ default upload dialog you can use the script below:

RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx
RewriteCond %{QUERY_STRING} !(^|&)coral-no-serve$
RewriteRule wp-content/uploads/(.*)$ http://www.yoursite.com.nyud.net/blog/wp-content/uploads/$1 [R,NC]

The first two lines are recommended on the Coral Cache wiki. The last line does the actual rewriting.

Put this in a .htaccess file in the root directory of your blog and observe the magic. The first time you load a page it might take a bit longer than usual, but with high traffic, this trick should quickly pay off. I’m not sure how often Coral flushes their cache, but if you have a low traffic site, this might actually slow down the loading a bit if the time between visits is larger than the lifetime of a file in the cache.

Of course the best part about using url rewriting is that you can easily switch it off at any time. When the digg effect subsides, or traffic goes down significantly, and you no longer need the cache you can simply comment out these 3 lines from your .htaccess file. In fact, you can just commend out the last line, as the two conditions can’t really do any damage by themselves.

Note that side effect of this trick is that you no longer need to worry about the hotlinking issue. Since all images will be redirected to the Coral Cache the lazy assholes can hotlink to you all they want. In fact you might as well stick the following two lines right after the Coral Cache conditionals:

RewriteCond %{HTTP_REFERER}!^$ 
RewriteCond %{HTTP_REFERER}!^http://(www\.)?yoursite\.com.*$ [NC]

First line excludes requests without a referrer, while the second one excludes the local requests. You may also want to add few lines here that will exclude Googlebot and other similar services which you may or may not want to index your images. But proper hotlinking prevention strategies are a a topic for a whole new discussion.

Then, when your server gets dugg comment out all the hotlinking related lines, and you go back into ultra-bandwidth saving mode.

Also, please feel free to correct or improve my mod_rewrite code. It is definitely not perfect, but it is functional. I would greatly appreciate any input or constructive criticism here. mrgreen

Update 08/17/2007 06:24:31 PM

Damn it! Phil Haack beat me to it. I wrote this post on the 14th and 15th completely independently from Phil. I was also inspired by the Yslow plugin to do some research on this topic.

Of course I decided to queue this post to appear on Monday the 20th (because I had some other stuff in the pipe) while Phil posted it right away. Oh well…

Update 08/20/2007 11:40:34 AM

Oops. It seems that Coral Cahce wiki got vandalized. Too bad. Let’s hope they get it fixed soon. I tried to restore it back to normal, but it seems that it has been edited by spammers so many times their wiki software “forgot” the good article already. (

Also it’s probably worth mentioning that this caching method should mostly be used for heavy lifting during immense traffic spikes. Using it to cache your images on every-day basis is probably not the best idea as it might and will slow down your page - see Phill Haacks post.

Also, imagered.com is another valid alternative here. You could use the same techniques outlined here to cache your images with their site.

Writing Your First Wordpress Plugin

Sunday, July 8th, 2007

I never wrote a wordpress plugin before, so when I set out to learn how they work, I was surprised to see how easy it is to create one. One would think that there would be lot’s of setup and a lot of digging in wordpress’ iinternals. But this is not the case. The Wordpress plugin API is surprisingly straightforward and clean. A simplest plugin you could think of would be this simple Hello World plugin:

<?php
/*
	Plugin Name: Helo World
*/ 
?>

What does this plugin do? Absolutely nothing, except for showing up in your plugin management screen:

Hello World
click to enlarge

Worpress scans every php file in the wp-content/plugins directory and parses through the comments in those files to create the entries in the plugin manager. You write the comments, and they magically appear in those boxes. I will show you a full comment block in my next example.

Let’s write a very simple plugin - a word filter for your comments. What is a word filter? It’s a script that will replace occurrences of one word with another. You can use word filters to censor obscenities from your comments, or just mess with your readers by swapping words like male and female, straight and gay, or perhaps if your blog is popular enough to attract the First Poster change “first” to “I’m a looser”. In my example I will try to switch foo into bar and test into done.

For that purpose I set up two fake comments on my test blog:

Original Comments

If the plugin works, all the foos and test should change to bars and dones. I will show you the plugin first, and then explain the tricky bit(s):

<?php
/*
Plugin Name: Test Plugin
Plugin URI: http://example.com/
Description: This plugin is Awesome.
Author: Lukasz Grzegorz Maciak
Version: 0.1
Author URI: http://terminally-incoherent.com/
*/ 
 
// plugin function
function test_wordfilter($text) 
{
 
	$words = array ( "foo " => "bar ",
				" test" => " done");
 
	foreach ($words as $key => $value)
	{
		$text = str_replace($key, $value, $text);
	}
 
 
	return $text;
}
 
// wordpress hook
add_filter('comment_text', 'test_wordfilter');
 
?>

I think the function is pretty straightforward - take a string, create array, loop through array, replace every $key with a corresponding $value, return a string. If you don’t get it, tough. Figure it out.

The line you want to pay attention to is this one:

add_filter('comment_text', 'test_wordfilter');

This is a wordpress hook. There are two types of hooks - actions and filters. Actions are triggered by various events such as publishing a post. Filters on the other hand let you manipulate the way data is displayed on the screen.The function add_filter takes two parameters.

The first parameter is the name of the filter. In our case it is comment_text because we only want to use our word filter in the comments. If we wanted to also apply this filter to blog posts we would need to add another filter like so:

add_filter('the_content', 'test_wordfilter');

You can find a full list of available filters and actions in the Wordpress Plugin API Refference.

As you probably guessed, the second parameter is the name of the function you defined in the plugin. Note that your function name should be unique, not to collide with other plugins and native wordpress functions. Most filters will return some desired data and send it as a parameter to your function.

Note that this plugin includes a full comment block with all the attributes filled out. If you look at your plugin manager, here is how it will show up:

Plugin Manager with the Test Plugin
click to enlarge

Now all we have to do is to activate the plugin. If it works, it should tell you that activation was successful, and the entry should become green. If you have some errors in your script, you will get a brief error message. Usually it’s something among the lines of “unable to activate the plugin due to fatal error”. This makes debugging your plugins bit difficult, so it’s a good idea to use an editor which checks PHP syntax as you type, like for example Komodo Edit. This will cut down on the number of silly typos, and forgotten semicolons in your code.

Once the plugin is activated, pop over to our comments section we can see that it worked:

Filtered Comments

Note that this plugin is far from perfect. For example it changes the word foobar into barbar which might not exactly be what we wanted. It’s probably a good idea to change the str_replace fucntion to preg_replace and use regular expressions to ensure that only the words we want get replaced. But that’s something you can do on your own.

Now you have a fully functional plugin to play around with. How easy was that?

Upgraded to Wordpress 2.0.5

Sunday, November 26th, 2006

This morning I decided to upgrade my wordpress version to 2.0.5. The whole thing went smoothly, but several of the plugins which require you to actually edit some of the Wordpress files broke. I think I fixed all of them, but if you notice any glitches, please let me know.

Also, my stylesheet was reset to the default version. I restored most of the custom styles that I added over time, but if something looks weird please post a comment in this post and I’ll fix it ASAP.

Finally, a hReview plugin that works!

Saturday, October 21st, 2006

You might have noticed that I have been using the hReview microformat for most of my movie and episode reviews. The format of my review blocks was constantly changing over the past few months, because sometimes the auto generators may be a little bit funky. But I think I finally found a formula that works thanks to Andrew E. Scott’s excellent wordpress plugin.

hReview Wordpress Plugin

Best hReview plugin I have seen so far

I have been looking for a nice, robust, and easy to use hReview Wordpress plugin for a while now. And I think I finally found it. Andrew E. Scott’s app is exactly what I needed.

I highly recommend it. If you want to jump on the microformat bandwagon this plugin will get you up and running in no time.

My rating: 5.0 stars
*****

So there you have it - a hReview review of a hReview generating plugin.