Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Redirect users to registration page and then to previous page? WordPress

  • SOLVED

Hi,

I have a nearly finished Wordpress and Woocommerce site, and just need a little help with redirecting users.

1. When users visit the following page ID's: 7002, 7676, 8353, 8578, I want them to first be checked if they're logged in and if they're not, redirect them to a custom registration page: http://www.poopydoo.com/user-registration/

2. When the user completes registration and clicks "Register" I want to redirect them to the previously visited page, (the page where they first came from.)

That's it. I hope I've explained myself clear enough.

I know my way around code, but I'm not a programmer. I know how to edit the functions.php file or template files within Wordpress for example, but I'm having trouble with this modification.

Let me know if you need access to the website because currently I'm redirecting visitors to our old website. I would need your IP address to have it white-listed.

Thank you all.

Answers (3)

2014-07-04

John Cotton answers:

I largely agree with DBranes but think it needs refining:


add_action( 'template_redirect', function(){
global $post;
if( ! is_user_logged_in() && in_array( $post->ID, array( 7002, 7676, 8353, 8578 ) ) ) {
wp_safe_redirect( 'http://www.poopydoo.com/user-registration/?redirect_to='.$post->ID );
exit();
}


Now, your registration page knows which page there were trying to get to.

At the end of successful registration (assuming it's a one page thing, otherwise you'll need to carry the redirect_to id along), you can do this:


wp_safe_redirect( get_permalink($_GET['redirect_to']) );
exit();


Rhonn comments:

Thanks John Cotton for your answer.

The first code worked out great. There's only one thing.
As I mentioned I have a Woocommerce site and the page ID's described earlier are product pages withing a Product Category. When I try to enter that product category: http://www.poopydoo.com/product-category/waste-cleanup/ I am redirected to the registration page with this URL: http://www.poopydoo.com/user-registration/?redirect_to=7676

As far as I know Woocommerce automatically generates the product category pages but I don't know why the script identifies the product category page as 7676. I want to be able to go to the product category page without being redirected. Any ideas?


And one last thing about the second bit of code.
I used Gravity Forms for my registration page, do I add the code to my theme's function file or to a file within gravity forms plugin directory? Like form_settings.php or gravityforms.php?

Thanks again.


John Cotton comments:

On the 2nd part of your question, definitely in the functions.php (or a custom file of your own - some themes have a place for custom code). You should never change a plugin's code.

There will be a hook that you can use triggered at the end of form processing (I don't use Gravity forms so I can't say what it would be).

On the 1st, you can pass anything you want in the query string.

So perhaps pass the current archive slug if that's easier. All that matters is that you indicate to the registration page which page to go back to.


Rhonn comments:

Thanks again for your answer John Cotton.
I've found a gravity forms hook called: [[LINK href="http://www.gravityhelp.com/documentation/page/Gform_confirmation"]]Gform confirmation[[/LINK]].
Here's the description: <em>This filter can be used to dynamically change the confirmation message or redirect URL for a form. </em>

I'm using this code based on an example I found in their page, but it's giving me a syntax error: <em>syntax error, unexpected 'add_filter' (T_STRING).</em>
Again, I'm not a programmer.


add_filter('gform_confirmation', 'redirect_previous', 10, 4);
function redirect_previous($confirmation, $form, $lead, $ajax) {
wp_safe_redirect( get_permalink($_GET['redirect_to']) );
exit();
}


Any help with this?


John Cotton comments:

There's nothing wrong with the code you posted. The error must come just before that. Probably a missing semi-colon.


Rhonn comments:

Thanks again John, there was indeed a semicolon missing. Sorry if my question has been dragging on.
I tried again the piece of code I posted earlier based on your proposed solution:

add_filter('gform_confirmation', 'redirect_prev', 10, 4);
function redirect_prev($confirmation, $form, $lead, $ajax) {
wp_safe_redirect( get_permalink($_GET['redirect_to']) );
exit();
}


But as it's written it doesn't work, the redirection doesn't happen and the user doesn't even gets registered.
Now I tried another example found elsewhere and the user does get registered but redirects to the same user registration page. Here's the code:

add_filter('gform_confirmation', 'redirect_prev', 10, 4);
function redirect_prev($confirmation, $form, $lead, $ajax){
$confirmation = array('redirect' => wp_get_referer());
return $confirmation;
}


Is there a way to combine this two bits of code so I could achieve what I want?
I will award you the $20 for your help so far, and please tell me if you feel your extended help deserves more.

Thanks!


John Cotton comments:

Clearly Gravity forms supports some sort of redirection (as I said earlier, I don't use it).

On the basis of what works, I suggest you try:


function redirect_prev($confirmation, $form, $lead, $ajax){
return array( 'redirect' => get_permalink($_GET['redirect_to']) );
}
add_filter('gform_confirmation', 'redirect_prev', 10, 4);


Rhonn comments:

Thanks John it finally worked!

2014-07-04

Dbranes answers:

Hi, you can try this for the first part:

/**
* Redirect non-logged-in users to the registration page
*/

add_action( 'template_redirect', function(){

if( ! is_user_logged_in()
&& is_page( array( 7002, 7676, 8353, 8578 ) ) )
{
wp_redirect( 'http://www.poopydoo.com/user-registration/' );
exit;
}

});


You should explain better part 2.

There are WordPress functions like <em>wp_get_referer()</em> and <em>wp_safe_redirect()</em> that might interest you.

Here's an example from the [[LINK href="http://codex.wordpress.org/Function_Reference/wp_get_referer"]]Codex[[/LINK]]:

if ( wp_get_referer() )
{
wp_safe_redirect( wp_get_referer() );
}
else
{
wp_safe_redirect( get_home_url() );
}


but we would have to place it into the correct hook, depending on your setup.


ps: notice that if you copy/paste code from this site, you get extra new lines.


Dbranes comments:

ps: It's a good idea from John Cotton to use the <em>redirect_to</em> GET parameter.


Rhonn comments:

Thanks, I'll check it out.

2014-07-04

Kyle answers:

What are you using for the user registration form?


Rhonn comments:

Yeah, sorry I didn't mentioned it in my original post, I'm using gravity forms.

Here's part of my answer to John Cotton's proposed solution:

I used Gravity Forms for my registration page, do I add the code to my theme's function file or to a file within gravity forms plugin directory? Like form_settings.php or gravityforms.php?


Kyle comments:

Gravity forms has a hook for redirects, so drop a function like below into your functions.php....

add_filter("gform_confirmation", "custom_confirmation", 10, 4);
function custom_confirmation($confirmation, $form, $lead, $ajax){

$confirmation = array("redirect" => wp_safe_redirect( wp_get_referer() ) ); //from Dbranes above

return $confirmation;
}


This would be for all forms, if you want to make it for a certain form you can change "gform_confirmation" to "gform_confirmation_1" and change that 1 to your form ID