Skip to content

Instantly share code, notes, and snippets.

@megmorsie
Last active December 17, 2015 15:44
Show Gist options
  • Save megmorsie/cc9846f04861a19f7547 to your computer and use it in GitHub Desktop.
Save megmorsie/cc9846f04861a19f7547 to your computer and use it in GitHub Desktop.
WordPress Stack Exchange: Flush_rewrite_rules not working when settings updated
<?php
/*
* Plugin Name: Stack: Flush_rewrite_rules not working when settings updated
* Plugin URI: http://wordpress.stackexchange.com/questions/210596/flush-rewrite-rules-not-working-when-settings-updated
* Description: Stack Exchange - WordPress Development
* Version: 1.0
* Author: Megan Morsie
* Author URI: http://megabyterose.com/
* License: GPL2
*/
/* START - Original */
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 );
}
// * Dummy Custom Post Type for Example
function qd_custom_post_types() {
$post_type_slug = "/" . 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');
/* END - Original */
?>
@megmorsie
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment