WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Go back two pages and refresh (9 posts)

  1. Wheelie111
    Member
    Posted 1 year ago #

    Hello,

    I have a login page on the frontend of my website. When a user is reading an article and wants to rate it or comment on it they need to log in so they go to the login page. After one is logged in they end up on the login page which now says : "You have successfully logged in".

    However I'd like them to be brought back to the article they were reading instead of the renew login page. How can I best do this?

    I tried to do it using Javascript with window.history.go(-2). That gets the user back to the article page but there is a small problem. I have a menu button linking to the log in page saying: "Log in". When an user is logged in the text of the button changes to "Log out".

    After window.history.go(-2) the button still says "Log in" instead of "Log out". After I refresh the page (F5) the correct button appears.

    So what I am looking for is a way to go back two pages and then refresh the page.

    Or is there a way I can add the document.referrer URL as hidden field to the form and send them back to it?

    Thanks in advance!

    Here's my code for the form:

    // login form fields
    function rcp_login_form_fields( $args = array() ) {
    
    	global $post;	
    
    	if (is_singular()) :
    		$pageURL =  get_permalink($post->ID);
    	else :
    		$pageURL = 'http';
    		if ($_SERVER["HTTPS"] == "on") $pageURL .= "s";
    		$pageURL .= "://";
    		if ($_SERVER["SERVER_PORT"] != "80") $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    		else $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    	endif;	
    
    	// parse the arguments passed
    	$defaults = array (
     		'redirect' => $pageURL,
     		'class' => 'rcp_form'
    	);
    	$args = wp_parse_args( $args, $defaults );
    	// setup each argument in its own variable
    	extract( $args, EXTR_SKIP );	
    
    	ob_start();
    
    		do_action('rcp_before_login_form');
    
    		if(!is_user_logged_in()) {
    
    			// show any error messages after form submission
    			rcp_show_error_messages(); ?>
    
    			<form id="rcp_login_form"  class="<?php echo $class; ?>" method="POST" action="<?php echo $pageURL; ?>">
    				<fieldset class="rcp_login_data">
    					<p>
    						<label for="rcp_user_Login"><?php _e('Username', 'rcp'); ?></label>
    						<input name="rcp_user_login" id="rcp_user_login" class="required" type="text"/>
    					</p>
    					<p>
    						<label for="rcp_user_pass"><?php _e('Password', 'rcp'); ?></label>
    						<input name="rcp_user_pass" id="rcp_user_pass" class="required" type="password"/>
    					</p>
    					<p class="rcp_lost_password"><a href="/register"><?php _e('Not yet a member? Join us now!', 'rcp'); ?></a></p>
    					<p class="rcp_lost_password"><a href="http://www.upoker.nl/wachtwoord-vergeten"><?php _e('Forgot your password?', 'rcp'); ?></a></p>
    					<p>
    						<input type="hidden" name="rcp_action" value="login"/>
    						<input type="hidden" name="rcp_redirect" value="<?php echo $redirect; ?>"/>
    						<input type="hidden" name="rcp_login_nonce" value="<?php echo wp_create_nonce('rcp-login-nonce'); ?>"/>
    						<input id="rcp_login_submit" type="submit" value="Login"/>
    					</p>
    				</fieldset>
    			</form>
    			<?php
    		} else {
    			echo __('You are succesfully logged in.', 'rcp') . ' <a href="' . wp_logout_url(home_url()) . '">' . __('Log out', 'rcp') . '</a>';
    		}
    
    		do_action('rcp_after_login_form');
    
    	return ob_get_clean();
    }
  2. simonhayre
    Member
    Posted 1 year ago #

    easy...

    add_action('init', 'rcp_login_form_fields_init');
    
    function rcp_login_form_fields_init(){
    
        if(!is_user_logged_in()) {
              auth_redirect();	//redirect to login page, this also handles redirecting
                                            // them back to the current page when they've logged-in
              exit;	//finished!
        }
    }

    if this isn't what your after, have a look at how this is achieved, you might be able to replicate the wordpress function i.e.:
    /wp-includes/pluggable.php Line 742

  3. Wheelie111
    Member
    Posted 1 year ago #

    Hello Simon,

    Thanks for you swift reply!

    I tried your solution, I assume it was meant for functions.php, but the entire
    website stopped working after I placed it in there.

    Did I do something wrong?

  4. simonhayre
    Member
    Posted 1 year ago #

    Yeah, I would place it in functions.php of your theme
    I just tried the code above and it just loop around.
    So we need to identify if the user is on a login page to break the redirect.
    Try this.

    add_action('init', 'rcp_login_form_fields_init');
    
    function rcp_login_form_fields_init(){
        if(!is_user_logged_in() && !rcp_login_form_fields_is_login_page()) {
              auth_redirect();	//redirect to login page, this also handles redirecting
                                            // them back to the current page when they've logged-in
              exit;	//finished!
        }
    }
    
    function rcp_login_form_fields_is_login_page() {
        return in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'));
    }

    see if this example gives you any joy.

  5. Wheelie111
    Member
    Posted 1 year ago #

    Hi,

    Thanks again but unfortunately it still crashes.
    Safari and Firefox give an error saying that the login page has too many redirects.

  6. simonhayre
    Member
    Posted 1 year ago #

    Thats strange, I'm not getting that error anymore...?
    double check that you've added the extra function and clause in the if statement.

    if(!is_user_logged_in() && !rcp_login_form_fields_is_login_page())

    if there's still no joy, debug it by putting the follow inside the rcp_login_form_fields_init function:

    var_dump(rcp_login_form_fields_is_login_page());
    exit;

    it should return true if the url is on wp-login.php or wp-register.php

  7. Wheelie111
    Member
    Posted 1 year ago #

    I must be doing something because it still crashes and burns with the same error. [ redacted, support is offered via the forum and not email or Skype. ]

  8. simonhayre
    Member
    Posted 1 year ago #

    but just check first that the code you are amending is the right file.
    if you remove all the code we just added, are you still getting the issue.

  9. Wheelie111
    Member
    Posted 1 year ago #

    Thanks again Simon!

Topic Closed

This topic has been closed to new replies.

About this Topic