Using WordPress ‘do_parse_request’ PHP filter

The do_parse_request WordPress PHP filter allows you to decide whether to parse an incoming request or not.

Usage

add_filter('do_parse_request', 'my_custom_parser', 10, 3);
function my_custom_parser($bool, $wp, $extra_query_vars) {
  // your custom code here
  return $bool;
}

Parameters

  • $bool (bool): Whether or not to parse the request. Default is true.
  • $wp (WP): The current WordPress environment instance.
  • $extra_query_vars (array|string): Extra passed query variables.

More information

See WordPress Developer Resources: do_parse_request

Examples

Disable request parsing for a specific query variable

Disable request parsing when the no_parsing query variable is set to true.

add_filter('do_parse_request', 'disable_parsing_for_query_var', 10, 3);
function disable_parsing_for_query_var($bool, $wp, $extra_query_vars) {
  if (isset($_GET['no_parsing']) && $_GET['no_parsing'] === 'true') {
    return false;
  }
  return $bool;
}

Disable request parsing for specific user roles

Disable request parsing for users with the subscriber role.

add_filter('do_parse_request', 'disable_parsing_for_role', 10, 3);
function disable_parsing_for_role($bool, $wp, $extra_query_vars) {
  if (current_user_can('subscriber')) {
    return false;
  }
  return $bool;
}

Disable request parsing for a custom URL

Disable request parsing when the URL contains a specific pattern.

add_filter('do_parse_request', 'disable_parsing_for_custom_url', 10, 3);
function disable_parsing_for_custom_url($bool, $wp, $extra_query_vars) {
  if (preg_match('/custom-pattern/', $_SERVER['REQUEST_URI'])) {
    return false;
  }
  return $bool;
}

Enable request parsing only for specific post types

Enable request parsing only when the post_type query variable is set to custom_post_type.

add_filter('do_parse_request', 'enable_parsing_for_custom_post_type', 10, 3);
function enable_parsing_for_custom_post_type($bool, $wp, $extra_query_vars) {
  if (isset($_GET['post_type']) && $_GET['post_type'] === 'custom_post_type') {
    return true;
  }
  return false;
}

Modify query variables before request parsing

Add or modify query variables before the request is parsed.

add_filter('do_parse_request', 'modify_query_vars_before_parsing', 10, 3);
function modify_query_vars_before_parsing($bool, $wp, $extra_query_vars) {
  if (isset($_GET['custom_var'])) {
    $wp->query_vars['custom_var'] = $_GET['custom_var'];
  }
  return $bool;
}