0
if(isset($_GET["link"])){
$link = $_GET["link"];
header("Location $link");
}

this my redirection codes. i put this code in function.php but not working.

Example link: http://localhost/?link=http://www.google.com

How can i do it for working?

1 Answer 1

2
function custom_function_redirect() {
if ( isset( $_GET[ 'link' ] ) && $_GET[ 'link' ] != '' ) {
    wp_redirect( $_GET[ 'link' ], 301 );
}else{
    die('test');
 }
}

add_action('template_redirect','custom_function_redirect');

Try this it worked for me.

For redirection you should hook the function in template_redirect

3
  • you'd better use ! empty( $_GET[ 'link' ] ) here php.net/manual/fr/function.empty.php instead of isset() and != '' and maybe wp_safe_redirect()
    – JMau
    Jun 19, 2016 at 10:16
  • That can work too
    – bagpipper
    Jun 19, 2016 at 10:16
  • 1
    What if $_GET doesn't exist? Think CLI. What if the value is an array? What if it contains malicious code?
    – fuxia
    Jun 19, 2016 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.