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 See comments below.themes.php
to plugins.php
and load-themes.php
to load-plugins.php
.
Enjoy.
I wouldn’t recommend using it as-is for the plugins as it will flush the rewrite rules when any plugin is activated. You can use something like:
if ( 'plugin.php' == $pagenow && isset( $_GET['activated'] ) && isset( $_GET['plugin'] && plugin_basename( __FILE__ ) == $_GET['plugin'] )
or use the activation hook to flush the rewrite rules
Hello Pete Mall,
I know you posted your comment long time ago. But, I have been working on WordPress since recently and have encountered a similar problem. I have rewrite rules in 2 different plugins and flush_rules() upon activation/deactivation of both these plugins. When I activate one plugin, it flushes all the rules of other plugin. How do I handle this?
Appreciate your reply.
Actually, plugins.php uses
activate=true
instead ofactivated
andplugin
will not be set. I would recommend using the activation hook for plugins to flush the rewrite rules (assuming the plugin is not being thrown in mu-plugins).Thanks. And good to know. I was following up on Michaels tweet regarding this issue.
So what’s the *preferred* method when using the mu-plugins folder?
This seems to be a common issue that nobody has found a good solution for.
A plugin may be installed/upgraded using svn/ftp/WordPress installer and there is no hook that will fire when a plugin is upgraded outside WordPress.
I would save a version number in the options table and update it with every new version of the plugin. Use this version number to run all installation and update procedures for the plugin. If the option does not exist then it’s a new installation.
Well one, why not a plugin activation hook? But anyway, I agree with Pete… I don’t really like activation hooks. They work okay for plugins, but they don’t scale particularly well (especially across networks). Themes don’t have them, and I’m not sure they ever will. They also don’t work for upgrades.
Thus it’s better to have an upgrade routine you can also leverage for activation. Use a version number in the options table and trigger the routine on admin_init (not on the frontend), as Pete says. A number of my plugins leverage this, for example — check out this one.
You’ll note I do use an activation hook there, but it’s only to set up an uninstall hook. On one hand, that’s also something that could be done on admin_init. On the other hand, I also am not a fan of uninstall hooks. (This is a particularly good use case for them, however.)
Austin, this post just saved me a metric f-ton of time that would have otherwise been spent fumbling through the Codex…thanks man!
Thanks that worked well.
is this still the best way? ive created a rewrite and want to do this without the user having the resave permalinks
Pretty much. Unless you check if an option exists then create it if not.
I actually went with the after_switch_theme hook
function reflush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action( ‘after_switch_theme’, ‘reflush_rules’ );
Hello,
I am using flush_rewrite_rules() in activation and deactivation of 2 different plugins and this is where I have a problem. When I activate plugin-1, the rewrite works fine. When I activate plugin-2, the rewrite of plugin-1 disappears. How do I fix this. Thanks a ton in advance.