D
D
drveb12021-06-13 21:56:42
JavaScript
drveb1, 2021-06-13 21:56:42

How to redirect url in WP depending on utm tag?

There is a WordPress site.

You need to do the following
1) If a person comes to the site via a link that has the required utm label, then the page opens. Suppose a person came via the link site.ru/fgh?link=886885969 , then the link will work and the page site.ru/fgh?link=886885968 will open (the value of the link itself does not matter, the main thing is that this utm label should be and have any value)
2) If he follows the link without utm link, he will be redirected to another page. For example, it will come from the link
site.ru/fgh or site.ru/fgh?link= in both cases, the desired page will not open, but will be redirected to the url specified in the settings.

How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Gorbunov, 2021-06-14
@drveb1

In the theme's functions.php file

add_action( 'template_redirect', 'toster_q_1004505_redirect' );

function toster_q_1004505_redirect() {

  if ( ( isset( $_GET['link'] ) && ! $_GET['link'] ) || ! isset( $_GET['link'] ) ) {

    $url = '/example'; // Куда переадресуем, если utm метка link пустая или отсутствует
    wp_safe_redirect( $url ); // либо функция wp_redirect($url); если нужно переадресовать на внешний url
    exit();

  }

}

Replace wp_safe_redirect($url) with wp_redirect($url) if you need to redirect to an external site.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question