Skip to content

Instantly share code, notes, and snippets.

@gboone
gboone / maybe_flush.php
Last active December 21, 2015 10:49
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 );
}
}