1

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.

2
  • There isn't enough information about what you're doing to answer this. We need to know what it is that you're doing in code that requires you to flush the rules, as it has a huge impact on wether it works or not. I know you'd prefer to keep things generic to hide what you're doing but it's unavoidable. We need to know which options you're updating, what it does, what changes you made, the hooks and the functions needed to reproduce the problem
    – Tom J Nowell
    Commented Apr 30, 2021 at 13:01
  • Thanks for commenting. I will update the question with more information. Thanks Commented Apr 30, 2021 at 13:07

1 Answer 1

2

flush_rewrite_rules() should be called very early within the Wordpress initialization. The reason is that the entpoints cannot be changed afterwards.

A solution could look like this:

function updateMySlugOption($old_value, $value, $option) {
    if( $old_value != $value ) {
        set_transient('update_slugs', 1);
    }
}
add_action( 'update_option_mySlug', 'updateMySlugOption', 10, 3);

function update_slugs() {
    if( get_transient('update_slugs') ) {
        flush_rewrite_rules();
        delete_transient('update_slugs');
    }
}
add_action('init', 'update_slugs');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.