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 badges1 Answer
Reset to default 1Try 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
版权声明:本文标题:php - Where to insert redirect code based on http_referer? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744662335a2618320.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论