[wp-trac] [WordPress Trac] #47526: Flagging system needed to help flush rewrite rules on post type registration from a plugin

WordPress Trac noreply at wordpress.org
Thu Jun 13 16:01:07 UTC 2019


#47526: Flagging system needed to help flush rewrite rules on post type
registration from a plugin
------------------------------------+------------------------------
 Reporter:  kevindees               |       Owner:  (none)
     Type:  feature request         |      Status:  new
 Priority:  normal                  |   Milestone:  Awaiting Review
Component:  Rewrite Rules           |     Version:  5.2.1
 Severity:  normal                  |  Resolution:
 Keywords:  dev-feedback has-patch  |     Focuses:
------------------------------------+------------------------------

Comment (by kevindees):

 How most plugins do it now (if they do not require to flush the permalinks
 manually).

 {{{#!php
 <?php
 // Plugin Name: My Plugin

 function my_plugin_post_types() {
     register_post_type('book', ['public' => true]);
 }

 add_action( 'init', 'my_plugin_post_types');

 function my_plugin_activate() {
     // oddly register "your" post types and
     // only "your" post types causing
     // bugs for others to deal with
     my_plugin_post_types();
     flush_rewrite_rules();
 }

 function my_plugin_deactivate() {
     // if post type is changes you
     // need to remember to update
     // the id here as well
     unregister_post_type('book');
     flush_rewrite_rules();
 }

 register_activation_hook(__FILE__, 'my_plugin_activate');
 register_deactivation_hook(__FILE__, 'my_plugin_deactivate');
 }}}

 Proposed solution: add a wp_options entry called _site_state_changed that
 can be checked for after the init action is called admin_init is also
 another option if init is too aggressive. Store an array of native wp or
 your plugin function names in the _site_state_changed entry.

 If _site_state_changed is not empty call the functions added to the entry
 on the init action.

 Doing this simplifies and fixes the bugs introduced by the current method
 devs take. Note, the proposed method is already being used by others via
 custom workarounds so this would remove the need for numerous
 implementations by those same people.

 {{{#!php
 <?php
 // Plugin Name: My Plugin

 add_action( 'init', function() {
     register_post_type('book', ['public' => true]);
 });

 function my_plugin_activate() {
     update_site_state_changed( 'flush_rewrite_rules' );
 }

 function my_plugin_deactivate() {
     update_site_state_changed( 'flush_rewrite_rules' );
 }

 register_activation_hook(__FILE__, 'my_plugin_activate');
 register_deactivation_hook(__FILE__, 'my_plugin_deactivate');

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47526#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list