Redirect to previous page after login?

This topic contains 4 replies, has 2 voices, and was last updated by  Guust 5 years, 4 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #452796

    Josh
    Expired Member
    Post count: 11

    Hello!

    I am hoping to set up a way to redirect a logged-out user back to the listing they came from after they log in, similar to how the claim listing button and add listing buttons work when a user is not logged in. This really only seems to be an issue in the “Leave a Review” link to the login page.

    In the geodirectory translation file, I found this snippet: “You must be logged in to post a comment.” Should the “%s” be changed to something else to allow for the redirect?

    Any help is appreciated! Thank you!

    #452797

    Guust
    Moderator
    Post count: 29970

    If you use our UsersWP plugin, you will find a setting that will do exactly that: https://userswp.io/

    https://userswp.io/docs/userswp-main-settings/#login

    Thanks

    #452940

    Josh
    Expired Member
    Post count: 11

    Thanks for the reply, Guust! What I am looking for is actually a redirect back to the previous listing after logging in, instead of a specific page.

    Here is some code I’m using for blog post comments, that is giving me the functionality I’m looking for.

    
    
    add_filter( 'comment_form_defaults', function( $fields ) {
        $fields['must_log_in'] = sprintf( 
            __( '<p class="must-log-in">You must <a href="%s">login</a> to post a comment.</p>' 
            ),
            geodir_login_url(array('redirect_to' => urlencode_deep(get_permalink()))) 
        );
        return $fields;
    });

    Is there a way to do this with reviews? I found this inside of reviews.php within the plugin files; can this be modified similarly?

    
    
    $args = apply_filters('geodir_review_form_args', array(
            'title_reply' => __('Leave a Review', 'geodirectory'),
            'label_submit' => __('Post Review', 'geodirectory'),
            'comment_field' => '<p class="comment-form-comment"><label for="comment">' . __('Review text', 'geodirectory') . ' <span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" required></textarea></p>',
            'must_log_in' => '<p class="must-log-in">' . sprintf(__('You must be <a href="%s">logged in</a> to post a comment.', 'geodirectory'), geodir_login_url()) . '</p>'
        ));
        comment_form($args);

    Thanks again for the help!

    #452941

    Josh
    Expired Member
    Post count: 11

    Thanks for the help, Guust! What I am actually hoping to accomplish is a redirect back to the listing, rather than to a specific page. Here is some code I’m using for the blog post comments, which gives me the functionality I’m looking for:

    
    
    add_filter( 'comment_form_defaults', function( $fields ) {
        $fields['must_log_in'] = sprintf( 
            __( '<p class="must-log-in">You must <a href="%s">login</a> to post a comment.</p>' 
            ),
            geodir_login_url(array('redirect_to' => urlencode_deep(get_permalink()))) 
        );
        return $fields;
    });

    Would you know if there is a way to do this similarly with reviews? I found the below code in the reviews.php, which seems to drive a url redirect as well, but behaves a bit differently than the code above. Would you know if there is a specific filter/hook I can use to modify it?

    
    
    $args = apply_filters('geodir_review_form_args', array(
            'title_reply' => __('Leave a Review', 'geodirectory'),
            'label_submit' => __('Post Review', 'geodirectory'),
            'comment_field' => '<p class="comment-form-comment"><label for="comment">' . __('Review text', 'geodirectory') . ' <span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" required></textarea></p>',
            'must_log_in' => '<p class="must-log-in">' . sprintf(__('You must be <a href="%s">logged in</a> to post a comment.', 'geodirectory'), geodir_login_url()) . '</p>'
        ));
        comment_form($args);

    Thanks again for the help!

    #452943

    Guust
    Moderator
    Post count: 29970

    That is what UsersWP does, try it:

Viewing 5 posts - 1 through 5 (of 5 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket