request

Home » Hooks » request

The "request" hook in WordPress is a crucial hook that is fired at the beginning of every HTTP request made to the website. It allows developers to intercept and modify the request before WordPress processes it further.

The "request" hook is often used to perform actions such as customizing the query parameters, manipulating the request headers, or even redirecting the user to a different page based on specific conditions. It provides developers with the flexibility to modify the request data and influence how WordPress handles it.

Here’s an example usage code for the "request" hook:

add_action( 'request', 'my_custom_request_handler' );

function my_custom_request_handler( $query_vars ) {
   // Modify the query parameters before WordPress processes it
   $query_vars['post_type'] = 'my_custom_post_type';

   // Perform additional checks or manipulations
   if ( isset( $_GET['custom_param'] ) ) {
      // Do something based on the custom parameter
   }

   return $query_vars;
}

In this example, we are hooking into the "request" action with the add_action() function and specifying our custom callback function my_custom_request_handler(). Inside the callback function, we can access and modify the query parameters by manipulating the $query_vars variable. In this case, we’re changing the post type to "my_custom_post_type". Additionally, we can perform additional checks on any custom parameters passed in the request and take appropriate actions based on them.

By using the "request" hook, developers have full control over the initial request made to WordPress and can customize it according to their specific needs.

Learn More on WordPress.org

WordPress snippets using the request hook

Register an account to save your snippets or go Pro to get more features.