Using WordPress ‘flush_rewrite_rules_hard’ PHP filter

The flush_rewrite_rules_hard WordPress PHP filter allows you to control whether a “hard” rewrite rule flush should be performed when requested.

Usage

add_filter('flush_rewrite_rules_hard', 'your_custom_function', 10, 1);

function your_custom_function($hard) {
    // your custom code here
    return $hard;
}

Parameters

  • $hard (bool): Determines if a “hard” flush should be performed. Default is true.

More information

See WordPress Developer Resources: flush_rewrite_rules_hard

Examples

Disable hard flush of rewrite rules

Prevent a hard flush of rewrite rules to avoid modifying the .htaccess or web.config file.

add_filter('flush_rewrite_rules_hard', 'disable_hard_flush');

function disable_hard_flush($hard) {
    return false;
}

Enable hard flush of rewrite rules for a custom post type

Perform a hard flush of rewrite rules when a specific custom post type is registered.

add_filter('flush_rewrite_rules_hard', 'enable_hard_flush_for_custom_post_type', 10, 1);

function enable_hard_flush_for_custom_post_type($hard) {
    if (post_type_exists('your_custom_post_type')) {
        return true;
    }
    return $hard;
}

Disable hard flush of rewrite rules for multisite

Disable hard flush of rewrite rules for multisite installations.

add_filter('flush_rewrite_rules_hard', 'disable_hard_flush_for_multisite');

function disable_hard_flush_for_multisite($hard) {
    if (is_multisite()) {
        return false;
    }
    return $hard;
}

Enable hard flush of rewrite rules for a specific plugin

Enable hard flush of rewrite rules when a specific plugin is active.

add_filter('flush_rewrite_rules_hard', 'enable_hard_flush_for_specific_plugin');

function enable_hard_flush_for_specific_plugin($hard) {
    if (is_plugin_active('your-plugin-folder/your-plugin-file.php')) {
        return true;
    }
    return $hard;
}

Enable hard flush of rewrite rules for specific user roles

Perform a hard flush of rewrite rules only for users with specific roles.

add_filter('flush_rewrite_rules_hard', 'enable_hard_flush_for_specific_roles');

function enable_hard_flush_for_specific_roles($hard) {
    if (current_user_can('manage_options') || current_user_can('editor')) {
        return true;
    }
    return $hard;
}