Flush_rewrite_rules not working when settings updated

Does this work for you? Seems to work when I try it.

  function qd_settings_api_init() {
  add_settings_section(
    'qd_theme_setting_section',
    'Theme Reading Settings',
    'qd_theme_setting_section_callback_function',
    'reading'
  );

  add_settings_field(
    'qd_news_page',
    'News Page',
    'qd_news_page_dropdown_cbf',
    'reading',
    'qd_theme_setting_section'
  );
  if (delete_transient('qd_flush_rules')) flush_rewrite_rules(); // * Added
  register_setting( 'reading', 'qd_news_page', 'qd_sanitize' ); // * Changed

}

/* Added this function. */
function qd_sanitize($input) {
    set_transient('qd_flush_rules');
    return $input;
}

add_action( 'admin_init', 'qd_settings_api_init' );

//FLUSH THE PERMALINKS WHEN THIS OPTION IS SAVED
function qd_flush_rules() { 
  flush_rewrite_rules(); 
  echo "News Page updated - " . get_option('qd_news_page');
} 

add_action('update_option_qd_news_page', 'qd_flush_rules');

function qd_theme_setting_section_callback_function() {
    echo "<p>Setting Instructions</p>";
}

function qd_news_page_dropdown_cbf() {
  $args = array(
    'depth'                 => 0,
    'child_of'              => 0,
    'selected'              => get_option( 'qd_news_page' ),
    'echo'                  => 1,
    'name'                  => 'qd_news_page',
    'id'                    => 'qd_news_page', // string
    'show_option_none'      => '&mdash; Select &mdash;', // string
    'show_option_no_change' => null, // string
    'option_none_value'     => '0', // string
  );
  wp_dropdown_pages( $args );
} 

//

function qd_custom_post_types() { 
    $post_type_slug = "https://wordpress.stackexchange.com/" .  get_page_uri( get_option('qd_news_page') );

    register_post_type( 'qd_news', 
        array(
            'labels' => array(
            'name' => __( 'Articles' ),
            'singular_name' => __( 'Article' )
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite'   => array( 'slug' => $post_type_slug . '/article', 'with_front' => false ),
    )
  );
}
add_action( 'init', 'qd_custom_post_types');

Leave a Comment