I am writing a plugin that instantiates a custom post type (among other things). It is a multisite plugin and lives in the directory mu-plugins.
What is the best practice for handling flush_rewrite_rules() in this situation? For a ‘normal’ plugin you’d do this in an activation hook — which is not going to be possible for a must-use plugin since those hooks are not available.
Since this is supposed to be a “one time” event after registering the custom post type, would it make sense to do something like this in my class that registers the CPT:
private function check_flush_my_CPT() {
global $wp_rewrite;
if ( !get_option('my_plugin_firstrun') ) {
$wp_rewrite->init();
$wp_rewrite->flush_rules(true);
update_option('my_plugin_firstrun', 'yes');
}
}
public function register_my_CPT() {
// do all the CPT setup steps for the $args array...
register_post_type('my_CPT', $args);
$this->check_flush_my_CPT();
}
add_action( 'init', array(&$this, 'register_my_CPT' ) );
So, the CPT registration happens on every ‘init’ action — but if I have this right, the rewrite rules flush only happens once. Ever.
Am I on the right track?
(edit): I just tried it; my CPT is giving a 404 not found error, so the rewrites rules are not working 🙁
(edit #2): I did try the solution for accessing the global variable as shown in this question: How to reliably flush rewrite rules on multisite? – I will update my code example above to show this. Unfortunately I am still getting 404 error when trying to load a CPT. I see that the rewrite rules are being stored in the database, it just seems like they are not being used. I’m lost.