Flush your rewrite rules on plugin/theme activation

I’ve actually been asked this a few times, and have seen others do it a few different ways. Anyway, here is my method:

/* Flush rewrite rules for custom post types. */
add_action( 'load-themes.php', 'frosty_flush_rewrite_rules' );

/* Flush your rewrite rules */
function frosty_flush_rewrite_rules() {
	global $pagenow, $wp_rewrite;

	if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
		$wp_rewrite->flush_rules();
}

Obviously, if you’re using this in a plugin you’d change themes.php to plugins.php and load-themes.php to load-plugins.php. See comments below.

Enjoy.