Last active
December 21, 2015 10:49
-
-
Save gboone/6294720 to your computer and use it in GitHub Desktop.
Tired of hitting the Save Permalinks button every time you register a post type in WordPress? Try this function: it grabs the cached rewrite rules, checks for a target (could be anything, but let's say it's a post type with an archive) and flushes the rules if the rewrite rule isn't present.
This file contains 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 | |
function maybe_flush_rewrite_rules($target) { | |
$rules = get_option( 'rewrite_rules' ); | |
if ( $rules[ $target . '/?$' ] != 'index.php?post_type=' . $target ) { | |
flush_rewrite_rules( $hard = true ); | |
} | |
} | |
// use it like this: | |
// first register your post type | |
function register_your_post_type( | |
$args = array( | |
//anything in here | |
'has_archive' => true | |
) | |
register_post_type( 'your_post_type', $args ) | |
); | |
// hook it to init | |
add_action( 'init', 'register_your_post_type' ); | |
// then pass the post type name to maybe flush rewrite rules | |
maybe_flush_rewrite_rules( 'your_post_type' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment