Using WordPress ‘got_url_rewrite’ PHP filter

The got_url_rewrite WordPress PHP filter allows you to modify the availability of URL rewriting in your WordPress installation.

Usage

add_filter('got_url_rewrite', 'your_custom_function');
function your_custom_function($got_url_rewrite) {
    // your custom code here
    return $got_url_rewrite;
}

Parameters

  • $got_url_rewrite (bool): Whether URL rewriting is available or not.

More information

See WordPress Developer Resources: got_url_rewrite

Examples

Disabling URL rewriting

Disable URL rewriting on your site.

add_filter('got_url_rewrite', 'disable_url_rewrite');
function disable_url_rewrite($got_url_rewrite) {
    return false;
}

Enabling URL rewriting

Force URL rewriting to be enabled on your site.

add_filter('got_url_rewrite', 'enable_url_rewrite');
function enable_url_rewrite($got_url_rewrite) {
    return true;
}

Enable URL rewriting only for logged-in users

Enable URL rewriting only for logged-in users, and disable it for non-logged-in users.

add_filter('got_url_rewrite', 'enable_url_rewrite_for_logged_in_users');
function enable_url_rewrite_for_logged_in_users($got_url_rewrite) {
    if (is_user_logged_in()) {
        return true;
    } else {
        return false;
    }
}

Enable URL rewriting based on a specific condition

Enable URL rewriting only if a specific condition is met, in this case, if the $some_condition variable is true.

add_filter('got_url_rewrite', 'enable_url_rewrite_based_on_condition');
function enable_url_rewrite_based_on_condition($got_url_rewrite) {
    $some_condition = true; // your condition
    if ($some_condition) {
        return true;
    } else {
        return false;
    }
}

Toggle URL rewriting using a custom function

Toggle URL rewriting using a custom function that checks for a certain condition.

add_filter('got_url_rewrite', 'toggle_url_rewrite_using_custom_function');
function toggle_url_rewrite_using_custom_function($got_url_rewrite) {
    if (your_custom_condition_function()) {
        return true;
    } else {
        return false;
    }
}

function your_custom_condition_function() {
    // Your custom condition logic here
    // Return true or false
}