How to process GET requests in WordPress?

Issue

I need to add several checks when user manually inputs URL with params on my site.

For example, if user types mysite.com/?random=text I want to redirect him to custom page (let’s imagine that I forbid using var random and I want users to be redirected in such cases).

How can I do this in WordPress?

Solution

add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
   $redirect_url = 'https://google.com/';

   if ( isset( $_GET['random' ]) && $_GET['random'] === 'text' && $_SERVER["REQUEST_URI"] == '/' ) {
      wp_redirect( $redirect_url , 404 );
      exit;
   }
}

Answered By – Artemy Kaydash

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published