1

I have the following code in my function.php:

add_filter('foo_registration_redirect', 'my_foo_registration_redirect', 12);

function my_foo_registration_redirect( $redirect_to) {
    if (is_page(1693)) {
         $redirect_to = '/my-page';
         return $redirect_to;
    }
    return $redirect_to;
}

The if statement is for some reason not true when I'm on the corresponding page. I also tried using

 if (is_page('page-name')) { ... }

which also never returns true, despite being on the right page. Any help is appreciated. The filter works as it should if I change the code to

function my_foo_registration_redirect( $redirect_to) {
         $redirect_to = '/my-page';
         return $redirect_to; 
}

However, I need the conditional, as I have 2 registration forms, on each their page, and one of them needs to point somewhere else.

10
  • What is the foo_registration_redirect filter? The correct one is registration_redirect
    – Laxmana
    Mar 14, 2017 at 13:47
  • 1
    Check and see what the $post object is at that point in time. It's possible that it's not set yet, hence there is no current page for is_page() to return. Do something like global $post; var_export($post); die(); just so it spits out the object on the page so you can see what it is. Mar 14, 2017 at 13:47
  • Also never hardcode the urls. Use get_the_permalink instead.
    – Laxmana
    Mar 14, 2017 at 13:48
  • The foo_registration_redirect is basically the same as the woocommerce redirect filter. @IndustrialThemes - When I do that in my function it returns NULL. But how would I go about checking what page the form is submitted from? And thanks for the heads up about the hardcoded url. Mar 14, 2017 at 13:57
  • 1
    Aha, so the $post object is not set yet when that filter runs for some reason. You can see where the form was submitted from using this: $_SERVER['HTTP_REFERER']; Mar 14, 2017 at 14:01

1 Answer 1

1

Changing the function to the following works, as it checks to see if the form was submitted from the correct page:

function my_foo_registration_redirect( $redirect_to) {
        if ($_SERVER['HTTP_REFERER'] !== get_the_permalink(1693)) {
            return $redirect_to;
        }
        $redirect_to = '/videoguide-startpakke';
        return $redirect_to;
}

Thanks a lot for the super helpful comments!

EDIT: Here's a slightly more polished solution

function my_foo_registration_redirect( $redirect_to) {
        $post_id = 1693;
        $post = get_post($post_id); 
        $slug = $post->post_name;

        if ($_SERVER['HTTP_REFERER'] !== get_the_permalink($post_id)) {
            return $redirect_to;
        }
        $redirect_to = $slug;
        return $redirect_to;
}

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.