Using ImageMagick for WordPress Thumbnail Generation

When you upload an image in WordPress, the blogging software tries to generate several scaled versions of the images uploaded. So after it’s done, WordPress provides 4 different sizes of the image for you to insert into the post – thumbnail, small, large, original.

I noticed that for my WordPress install, only the original image was available and the other options were disabled. The reason is that the PHP on my server was compiled by me myself and I didn’t compile many of the extensions. WordPress uses GD extension for thumbnail generation. But this time I didn’t recompile PHP with GD (I didn’t know how to compile GD alone as an extension). Instead I used ImageMagick and here’s the whole process.

Continue reading Using ImageMagick for WordPress Thumbnail Generation

Deleting the MT-powered blog

Months ago, I started a blog about interesting advertisements powered by Movable Type. During the installation and usage process, I got some knowledge about Movable Type, but the fact is that I’m still not quite familiar with it. Besides, the chosen topic is not quite related to my daily life. So now I’m stopping and deleting the blog.

Movable Type is a great blogging platform, and the team has been inventing many techniques and concepts, but the problem is that it’s defeated by WordPress because it’s not as simple and WordPress now has much more plugins.

The only advantage of using MT is that you don’t need to worry about the server load (digg effect, for example), but this is not a problem for us “little” bloggers. After several weeks, I even didn’t remember the url of the administration area.

I may start another blog, powered by WordPress, of course :)

Showing Post Title in Neighbor Post Preview

A few days back, a friend asked me whether the neighbor post preview plugin can be customized to show the post title in preview.

Yes, that’s a wonderful idea. Many WordPress users just use “next post” or “previous post” as the navigation link text instead of the post title. This can save much space and readers can more easily focus on the title of current post (example page). If you noticed, Live Spaces are using left/right arrow icons as the link text.

So I implemented this idea into my plugin. By doing this, it becomes more useful for those people who didn’t show the post titles in the link text.

Post title in preview

Thanks, Penelope, for this great idea.

Go to the plugin homepage for download. When activated, go to the options page for customization (http://yourdomain/wp-admin/options-general.php?page=neighbor-post-preview.php).

Perfect Excerpt in the WordPress Way

When I started working on the Neighbor Post Preview plugin, I expected WordPress to have a built-in function to get an excerpt from an arbitrary post. But it doesn’t have one, sadly.

There is only an the_excerpt tag for use within The Loop. It’s defined in wp-includes/post-template.php (version 2.3.2 code):

function the_excerpt() {
	echo apply_filters('the_excerpt', get_the_excerpt());

function get_the_excerpt($deprecated = true) {
	global $id, $post;
	$output = '';
	$output = $post->post_excerpt;
	if ( !empty($post->post_password) ) { // if there's a password
		if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $post->post_password ) {  // and it doesn't match the cookie
			$output = __('There is no excerpt because this is a protected post.');
			return $output;

	return apply_filters('get_the_excerpt', $output);

See the bad code? I don’t know who wrote this, but the line “$output = $post->post_excerpt;” should be placed after the if block. OK, this is out of topic :) .

Generally the function retrieves the explicit post excerpt and apply filters “get_the_excerpt” and “the_excerpt”. What is the explicit excerpt? When you’re writing or editing a post, its input is right below your editing area. I never wrote one, though. Most bloggers don’t write excerpts, I believe. So what happens when we apply filter “get_the_excerpt” to an empty string?

Let’s have a look at the code in wp-includes/default-filters.php:

add_filter('the_excerpt', 'wptexturize');
add_filter('the_excerpt', 'convert_smilies');
add_filter('the_excerpt', 'convert_chars');
add_filter('the_excerpt', 'wpautop');
add_filter('get_the_excerpt', 'wp_trim_excerpt');

By default, the string is processed only by the function wp_trim_excerpt (defined in wp-includes/formatting.php) before applying “the_excerpt” filters:

function wp_trim_excerpt($text) { // Fakes an excerpt if needed
	global $post;
	if ( '' == $text ) {
		$text = get_the_content('');
		$text = apply_filters('the_content', $text);
		$text = str_replace(']]>', ']]>', $text);
		$text = strip_tags($text);
		$excerpt_length = 55;
		$words = explode(' ', $text, $excerpt_length + 1);
		if (count($words) > $excerpt_length) {
			array_push($words, '[...]');
			$text = implode(' ', $words);
	return $text;

If the string is empty, the excerpt is faked using the content of the global $post. After that, the excerpt is filtered by some functions to make some “clean” text.

So I wrote this helper function for use in the plugin:

function tlnpp_excerpt($text, $excerpt_length = 55) {
	$text = str_replace(']]>', ']]>', $text);
	$text = strip_tags($text);
	$words = explode(' ', $text, $excerpt_length + 1);
	if (count($words) > $excerpt_length) {
		array_push($words, '[...]');
		$text = implode(' ', $words);
	return apply_filters('the_excerpt', $text);

This function truncate an arbitrary piece of text to 55 words by default, and process the result using the default WordPress filter functions (converts smiley’s, for example). It truncates text by words not characters, and the result is safe.

For more information, have a look at the code of the plugin. It’s very simple. Any suggestions?

Fighting WordPress Spams

Of course the most popular plugin for protecting WordPress blogs from spamming is Akismet. Yes it is simple to use, famous, and easy to get.

But in my opinion, if you’re running an unknown blog like mine, you should discard Akismet and choose a more clever one. Akismet blocks non-spam comments now and again, and it’s difficult to rescue them from within the thousands of spams. Some day you will suddenly find a regular comment by your friend in the spams and realize that. It’s the same situation when using Spam Karma or Bad Behavior. They all block regular comments by some probability.

So stay away with the plugins which recognize spams by using machines’ intelligence. I need a plugin that will block all spam bots but will never block a human being.

Usually, the spam bots are not clever enough to emulate a browser (except the human spammers, of course). So why not use some simple JavaScript to confirm the commenter is not a machine?

I first tried WordPress Hashcash. It blocks all bots and never blocks a human. But it needs a database table to function right. I don’t like this.

Then I found bcSpamBlock, which seems an ideal solution for me:

  1. It ensures only comments by human pass through the validation.
  2. It doesn’t need a database table.
  3. It rejects the spam in the “preprocess_comment” filter, which means the spam won’t be saved to the database, making the database always clean. Too many comment_ID’s we’ve wasted on spams! (Yes I’m an idealist :) )

There are some human spams which will bypass the plugin but I have the time to delete them.

Wait, still there are trackback spams! bcSpamBlock does have a simple way to check trackbacks, but I disabled this feature and used the Simple Trackback Validation plugin. The ideas to validate trackbacks are identical, but the latter is more reliable.

So the final solution becomes bcSpamBlock + Simple Trackback Validation.

When bcSpamBlock is famous enough, there must be a way for bots to analyze the keys and simulate a human (I’ll try this). But for now it’s enough for me.