[wp-hackers] Stripping attachment handling from rewrite rules

Johan Eenfeldt johan.eenfeldt at gmail.com
Sat Feb 7 16:48:45 GMT 2009


Inspired by the recent discussion of permalinks on wp-testers I took a
closer look at my rewrite_rules.

We use %postname% as permalink, which result in verbose rewrite_rules
-- in our case ~8000 rules (~350KB).

Looking a bit closer, most of that was various rewrites handling
attachments -- all pictures on all pages got a few each. This is done
to allow linking to and using them the same way as posts. Something we
do not use.

So I tried just strippin them:

function filter_rewrite_attachment($content) {
	if (!is_array($content))
		return $content;

	foreach ($content as $key => $val) {
		if (strpos($val, 'attachment') !== false)
			unset($content[$key]);
	}

	return $content;
}
add_filter('page_rewrite_rules', 'filter_rewrite_attachment');
add_filter('post_rewrite_rules', 'filter_rewrite_attachment');

This got the size of rewrite_rules down to ~300 rules (~35KB), and
seems to work just fine.

We didn't really have any problems (yet?) by the proliferation of
rewrite rules -- though changing it decreases the average mysql
transfers by almost 50%!

Anyone knows any reason not to do this?

Thank you,
Johan Eenfeldt


More information about the wp-hackers mailing list