Using WordPress ‘block_local_requests’ PHP filter

The block_local_requests WordPress PHP filter allows you to control whether local HTTP API requests should be blocked or not.

Usage

add_filter('block_local_requests', 'your_custom_function_name');
function your_custom_function_name($block) {
  // your custom code here
  return $block;
}

Parameters

  • $block (bool): Indicates whether to block local requests. Default is false.

More information

See WordPress Developer Resources: block_local_requests

Examples

Block all local requests

In this example, we will block all local requests by returning true.

add_filter('block_local_requests', 'block_all_local_requests');
function block_all_local_requests($block) {
  return true;
}

Allow all local requests

In this example, we will allow all local requests by returning false.

add_filter('block_local_requests', 'allow_all_local_requests');
function allow_all_local_requests($block) {
  return false;
}

Block local requests only on a specific page

In this example, we will block local requests only on a specific page by checking the current page ID.

add_filter('block_local_requests', 'block_local_requests_on_specific_page');
function block_local_requests_on_specific_page($block) {
  if (is_page(42)) {
    return true;
  }
  return $block;
}

Block local requests for a specific user role

In this example, we will block local requests for users with the ‘subscriber’ role.

add_filter('block_local_requests', 'block_local_requests_for_subscribers');
function block_local_requests_for_subscribers($block) {
  if (current_user_can('subscriber')) {
    return true;
  }
  return $block;
}

Block local requests based on a custom condition

In this example, we will block local requests based on a custom condition (e.g., a specific query parameter).

add_filter('block_local_requests', 'block_local_requests_based_on_condition');
function block_local_requests_based_on_condition($block) {
  if (isset($_GET['custom_param']) && $_GET['custom_param'] == 'block') {
    return true;
  }
  return $block;
}