WordPress Latest Entries

Linux Kernel, Modules and Plugins

Here are some views expressed by Linus Torvalds about the Linux kernel, plugins and how he thinks about derivative work. Very relevant to what I said about plugin interfaces and user space.

So, do themes and plugins serve a system-level or a user-level function?

p.s. Some have compared WordPress plugin and theme API to the way Linux loads kernel modules, when in fact relating it to system call interface (SCI) would be more appropriate.

GPL Sockets

It is interesting how thinking and understanding of ideas change over time. This morning I woke up and started reading the GPL licence with the intent to take another look at what it actually stands for.

I have come to conclusion that its purpose is to give everyone the freedom to do whatever they want with my work as long as they retain the freedom to derive from it.

With every software there are only two things one can do with it — either run it or modify it. By applying the GPL licence to my original work I am making sure that these two things can always happen and nobody can take those freedoms away. Read more »

Automatic Updates for Plugins and Themes Hosted Outside WordPress Extend

Here are two sample scripts along with an API to provide automatic updates for plugins and themes you host on your own server.

Inside /api you’ll find index.php which processes all the update requests. You should place this in something like http://updates.example.com and update $api_url in /plugin/test-plugin-update/test-plugin-update.php and /theme/portfolio-racer/inc/updates.php accordingly. If you activate these sample plugins without changing API URL, updates will be checked against my test server. If you decide to update, both plugin and theme will be replaced with exactly the same version of each.

AJAX Cache Purge Cookie Plugin

This plugin sets a “version” cookie of your site’s content, which can be used for time-based cache invalidation, as the cookie is checked and updated (if necessary) on each page request through a single AJAX request.

Download: ajax-cache-purge.zip (June 26, 2010)

Installation

Please note that this plugin is intended for people who run their own servers.

  1. Upload and enable the plugin.
  2. Add the value of wp_cache_key_cookie to the cache key.

Nginx Example

fastcgi_cache_path 	/var/www/cache  levels=1:2
			keys_zone=wp-cache:10m
			inactive=2m max_size=2000m;
fastcgi_temp_path 	/var/www/cache/tmp;
server {
	# other config options
	location ~ \.php$ {
		# Cookie is supplied by the plugin
		set $wp_cache_key_cookie 0;
		if ($http_cookie ~* "wp_cache_key_cookie[^=]*=([^;]+)(;|$)") {
			set $wp_cache_key_cookie wp_cache_key_cookie_$1;
		}
		set $wp_cache_key $scheme$host|$request_uri|args=$args$|$wp_cache_key_cookie;
		#add key in header for debugging
		#add_header	WP_KEY $osc_cache_key;
		fastcgi_cache 		wp-cache;
		fastcgi_cache_key 	$wp_cache_key;
	}
}

PHP APC Potential Cache Slam Averted for Key

This server uses PHP 5.3 with PHP-FPM (FastCGI Process Manager) patch and APC opcode cache. Several minutes after activating APC, I noticed that some pages stopped loading. Turns out it was because of an APC timebomb bug which is when all cache writes got locked because of expiring cache entries and new writes happening at the same time. The temporary solution is to add apc.write_lock = 1 in php.ini.

Notes on the 2010 WordPress Theme

Here are some of my random thoughts after going through the files of the new TwentyTen (2010) theme which will be introduced in WordPress 3.0.

header.php isn’t poetry at all

The content of the <title> should be generated within functions.php by using the wp_title filter, so that plugins and users can overwrite it. In header.php we would have only:

<title><?php wp_title('|', true, 'right'); ?></title>

and in functions.php something like:

add_filter('wp_title', 'twentyten_title', 10, 2);
function twentyten_title($title, $sep) {
	global $post;
	$title = wptexturize($title);
	$page_no = get_query_var('paged');
	if (is_front_page())
		$title = get_bloginfo('title') . ' ' . $sep . ' ' . get_bloginfo('description');
	elseif (!is_feed())
		$title .= ' ' . get_bloginfo('title');
	if ($page_no > 1)
		$title .= ' (page ' . $page_no . ')';
	return $title;
}

Theme’s stylesheet style.css should be loaded after the wp_head() call, so that users can overwrite styles added by the plugins. Read more »

Smarter Cleaner Gallery

Justin Tadlock’s Cleaner Gallery plugin fixes the semantics of the WordPress built-in gallery feature by placing all of the gallery related CSS in an external file instead of the inline CSS produced by WordPress core.

However, the problem is that this CSS file is loaded on every page request. Therefore, until WordPress supports combining all CSS files into one, here is a quick way to hide this CSS file on all pages that don’t contain a gallery (line 128 of cleaner-gallery.php):

Note: this will add cleaner-gallery.css only on single post and page views (not indexes or archives).

add_action('wp_head', 'cleaner_gallery_head', 0);
function cleaner_gallery_head() {
	global $post;
	if (strstr($post->post_content, '[gallery'))
		wp_enqueue_style( 'cleaner-gallery', CLEANER_GALLERY_URL . '/cleaner-gallery.css', false, 0.7, 'all' );
}

Hopefully, Justin can incorporate this into the next release of this handy plugin.

Automatically Escape HTML Entities of Code Fragments in Comments

Update: Ryan has made this into a plugin — Code Comments.

Add this to your theme’s functions.php to allow readers post fragments of code in their comments (wrapped in <code>...</code>) which are automatically encoded (think of < and &lt;)

add_filter('pre_comment_content', 'encode_code_in_comment');
function encode_code_in_comment($source) {
  $encoded = preg_replace_callback('/<code>(.*?)<\/code>/ims',
  create_function(
    '$matches',
    '$matches[1] = preg_replace(
        array("/^[\r|\n]+/i", "/[\r|\n]+$/i"), "",
        $matches[1]);
      return "<code>" . htmlentities($matches[1]) . "</code>";'
  ),
  $source);
  if ($encoded)
    return $encoded;
  else
    return $source;
}

Worth noting:

  • Everything wrapped in <code>...</code> is encoded.
  • Line breaks after opening <code> and before closing </code> are removed in order to avoid unnecessary <br /> tags.