Will someone please explain to me why flushing doesn't work in my case. I added a custom settings page with a default value, but it doesn't work when I hit save changes.
I use the admin_init
hook to register settings like this.
register_setting(
'my_general',
'my_general_settings',
array(
'default' => 'hello',
)
);
I added sections and fields, and those work. Now I am having a problem with this. If I update or save the settings, the update_option_my_general_settings
doesn't work. It isn't flushing the rules.
Here is the function code I used inside this hook.
if ( $old_value === 'hello' || $value === 'hello' ) {
// Flush rules
flush_rewrite_rules();
}
Remember I am supplying these arguments to the function $old_value, $value, $option
The filter mod_rewrite_rules
adds the data to .htaccess after I manually refresh the permalinks inside settings. But the flush_rewrite_rules()
doesn't seem to be refreshing the permalinks automatically.
What I want is that when the user updates the settings, the flush_rewrite_rules
should be populated.
More Info
I basically want to add headers like HSTS, XSS through the WordPress filter mod_rewrite_rules.
I know I can add these headers by using the WordPress filter 'wp_headers,
but the filter doesn't work in some cases. So I created a settings page. Inside which I created sections and fields. Whenever the user saves the settings I registered with register_setting(),
the mod_rewrite_rules
filter is called to add necessary data to the .htaccess
file. As we will add these headers programmatically, we need to flush the rewrite rules. So, what I did, I found this hook 'update_option{$option}
'. The callback function of this hook stores the flush_rewrite_rules()
function. Whenever the user sets the value hello,
the flushing should occur so the data inside the filter will be added to the .htaccess file. I can't post the code because if I do so, the question will be more code than a question. So I tried to keep the question short and to the point.
Here is the code I think would matter. I skipped the settings fields and sections HTML.
<?php
add_action('admin_menu', 'add_settings_page' );
add_action('admin_init', 'register_settings_sections_and_fields' );
add_action( 'update_option_hello_general_settings', 'hello_flush_rewrite_rules' , 10, 3 );
function add_settings_page()
{
add_options_page('Hello Settings', 'Hello', 'manage_options', 'hello', 'hello_settings_markup' );
}
function register_settings_sections_and_fields()
{
register_setting(
'hello_general',
'hello_general_settings',
array(
'default' => 'filter',
)
);
add_settings_section( 'hello_general_section', '', null, 'hello_general' );
add_settings_field( 'hello_general_settings_method', 'Select Method', 'hello_general_settings_method_callback', 'hello_general', 'hello_general_section' );
}
function hello_settings_markup()
{
?>
<form method="post" action="options.php">
<?php
settings_fields( 'hello_general' );
do_settings_sections( 'hello_general' );
submit_button();
?>
</form>
<?php
}
function hello_flush_rewrite_rules( $old_value, $value, $option )
{
if ( $old_value === 'hello' || $value === 'hello' ) {
// Flush rules
flush_rewrite_rules();
}
}
Note: The data gets added to .htaccess after I manually update the permalinks through the dashboard.
I really appreciate any help you can provide.