Last active
December 17, 2015 07:39
-
-
Save sang4lv/5574910 to your computer and use it in GitHub Desktop.
CPT Rewrite Flush
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_action( 'registered_post_type', 't5_silent_flush_cpt', 10, 2 ); | |
| add_action( 'registered_taxonomy', 't5_silent_flush_tax', 10, 3 ); | |
| /** | |
| * Flush rules for custom post types. | |
| * | |
| * @wp-hook registered_post_type | |
| * @param string $post_type | |
| * @param stdClass $args | |
| * @return void | |
| */ | |
| function t5_silent_flush_cpt( $post_type, $args ) | |
| { | |
| if ( $args->_builtin ) | |
| return; | |
| if ( ! $args->public ) | |
| return; | |
| if ( ! $args->publicly_queryable ) | |
| return; | |
| if ( ! $args->rewrite ) | |
| return; | |
| $slug = $post_type; | |
| if ( isset ( $args->rewrite['slug'] ) && is_string( $args->rewrite['slug'] ) ) | |
| $slug = $args->rewrite['slug']; | |
| $rules = get_option( 'rewrite_rules' ); | |
| if ( ! isset ( $rules[ $slug . '/?$'] ) ) | |
| flush_rewrite_rules( FALSE ); | |
| } | |
| /** | |
| * Flush rules for custom post taxonomies. | |
| * | |
| * @wp-hook registered_taxonomy | |
| * @param string $taxonomy | |
| * @param string $object_type | |
| * @param array $args | |
| * @return void | |
| */ | |
| function t5_silent_flush_tax( $taxonomy, $object_type, $args ) | |
| { | |
| // No idea why we get an array here, but an object for post types. | |
| // Objects are easier to write, so ... | |
| $args = (object) $args; | |
| if ( $args->_builtin ) | |
| return; | |
| if ( ! $args->public ) | |
| return; | |
| if ( ! $args->rewrite ) | |
| return; | |
| $slug = $taxonomy; | |
| if ( isset ( $args->rewrite['slug'] ) && is_string( $args->rewrite['slug'] ) ) | |
| $slug = $args->rewrite['slug']; | |
| $rules = get_option( 'rewrite_rules' ); | |
| if ( ! isset ( $rules[ $slug . '/(.+?)/?$'] ) ) | |
| flush_rewrite_rules( FALSE ); | |
| } | |
| //Left Open Intentionally |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment