Skip to content

Instantly share code, notes, and snippets.

@gboone
Last active December 21, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gboone/6294720 to your computer and use it in GitHub Desktop.
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.
<?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