WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#17325 closed defect (bug) (worksforme)

No good way to flush rules on registering post types

Reported by: WraithKenny Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Permalinks Keywords:
Focuses: Cc:

Description

Plugins that register post types can't flush_rewrite_rules() on activation.

flush_rewrite_rules is too early (for post types registered at init) at do_action( 'activate_' . $plugin, $network_wide ); and adding a delayed action on init fails because shortly after activate_plugin() there's a wp_redirect() at http://core.trac.wordpress.org/browser/trunk/wp-admin/plugins.php#L61 (which sets a new code execution) before the init hook runs.

What's left is manually visiting options-permalinks.php, setting an option and checking to do the flush, or flushing every page load, as is being recommended on many blogs.

Change History (2)

comment:1 scribu3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Here's how I do it:

function my_cpt_init() {
  register_post_type( ... );
}
add_action('init', 'my_cpt_init');

function my_rewrite_flush() {
  my_cpt_init();
  flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'my_rewrite_flush');

comment:2 WraithKenny3 years ago

Awesome, thanks

Note: See TracTickets for help on using tickets.