The parse_request WordPress PHP action fires once all query variables for the current request have been parsed.
Usage
add_action('parse_request', 'your_custom_function');
function your_custom_function($wp) {
// Your custom code here
}
Parameters
$wp(WP): Current WordPress environment instance (passed by reference).
More information
See WordPress Developer Resources: parse_request
Examples
Redirect to custom page template
This example checks if a specific query parameter exists, and if it does, it redirects to a custom page template.
add_action('parse_request', 'redirect_to_custom_template');
function redirect_to_custom_template($wp) {
if (isset($wp->query_vars['custom_param'])) {
include get_template_directory() . '/custom-page-template.php';
exit;
}
}
Detecting custom URL
This example checks for a custom URL path and performs an action based on the path.
add_action('parse_request', 'detect_custom_url');
function detect_custom_url($wp) {
if ($wp->request === 'custom-url') {
// Your custom code here
}
}
Add custom query variable
This example adds a custom query variable to the list of recognized query variables.
add_filter('query_vars', 'add_custom_query_var');
function add_custom_query_var($vars) {
$vars[] = 'custom_var';
return $vars;
}
add_action('parse_request', 'process_custom_query_var');
function process_custom_query_var($wp) {
if (isset($wp->query_vars['custom_var'])) {
// Your custom code here
}
}
Redirect users based on their role
This example redirects users to a specific page based on their user role.
add_action('parse_request', 'redirect_users_by_role');
function redirect_users_by_role($wp) {
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
if (in_array('subscriber', $current_user->roles)) {
wp_redirect('/subscriber-dashboard/');
exit;
}
}
}
Restrict access to a specific page
This example restricts access to a specific page and redirects non-authorized users to a different page.
add_action('parse_request', 'restrict_specific_page');
function restrict_specific_page($wp) {
if (is_page('restricted-page') && !current_user_can('manage_options')) {
wp_redirect('/error/');
exit;
}
}