admin管理员组

文章数量:1389865

If the client arrives at my WordPress site from a specific other domain, I want to direct the client to a specific page on my site. Preferably, I would like to use the PHP header function to perform the redirect, but non-coding solutions will be considered.

Right now I have placed conditional code at the very top of my active theme's header.php file, and if the $_SERVER['HTTP_REFERER'] == "some.domain" then I perform the redirect using a common html meta refresh, like so:

if($_SERVER['HTTP_REFERER'] == "/"){
    ?><meta http-equiv="refresh" content="0; url=/"><?php
}

The PHP header("Location: /my-custom-page/") doesn't work there, presumably because content output has already occurred. Meta refresh does the job for now, but it is very crude. My main problem with using meta refresh is that even with "0" seconds, the page content is already starting to load before the client is redirected to the special page I really wanted them to be on in the first place. So a user gets to see the masthead and other parts of the homepage for a couple of seconds, then they are taken to the page I want them to be on.

WordPress executes many files and functions even before it loads header.php. I want to know where I can put a PHP header redirect to a relative page in my WordPress (assuming there is no feasible non-coding solution). I don't want to modify wp-admin or wp-include files, if possible.

If the client arrives at my WordPress site from a specific other domain, I want to direct the client to a specific page on my site. Preferably, I would like to use the PHP header function to perform the redirect, but non-coding solutions will be considered.

Right now I have placed conditional code at the very top of my active theme's header.php file, and if the $_SERVER['HTTP_REFERER'] == "some.domain" then I perform the redirect using a common html meta refresh, like so:

if($_SERVER['HTTP_REFERER'] == "https://www.somewebsite/"){
    ?><meta http-equiv="refresh" content="0; url=https://my.wordpresssite/my-custom-page/"><?php
}

The PHP header("Location: /my-custom-page/") doesn't work there, presumably because content output has already occurred. Meta refresh does the job for now, but it is very crude. My main problem with using meta refresh is that even with "0" seconds, the page content is already starting to load before the client is redirected to the special page I really wanted them to be on in the first place. So a user gets to see the masthead and other parts of the homepage for a couple of seconds, then they are taken to the page I want them to be on.

WordPress executes many files and functions even before it loads header.php. I want to know where I can put a PHP header redirect to a relative page in my WordPress (assuming there is no feasible non-coding solution). I don't want to modify wp-admin or wp-include files, if possible.

Share Improve this question edited Mar 17, 2020 at 12:54 TARKUS asked Mar 17, 2020 at 12:47 TARKUSTARKUS 13710 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try the template_redirect hook (in your functions.php):

add_action( 'template_redirect', function () {
    if ( wp_get_raw_referer() === 'https://www.somewebsite/' ) {
        wp_redirect( home_url( 'my-custom-page/' ) );
        exit;
    }
});

本文标签: phpWhere to insert redirect code based on httpreferer