1

When a customer wants to leave a review on the product page, they are prompted to log in if not. Do you have a proposal to send the customer to the a specific login page and then return once logged in? I tried this but without success, I am still sent to my account page.


add_filter(‘login_url’, ‘custom_login_url’, 10, 3);

function custom_login_url( $login_url, $redirect, $force_reauth ) {

$login_page_id = get_permalink(woocommerce_get_page_id(‘login’));

return get_permalink( $login_page_id );

}
4
  • 1
    How are you prompting the user to log in if they're not already logged in? If you're redirecting users to WordPress's login URL, using wp_login_url(), you can send the URL you want them to return to (once they're logged in) as the $redirect parameter.
    – Pat J
    Commented Mar 30 at 19:54
  • Lorsque le client se trouve sur la page produit et qu'il souhaite laisser un avis il alors est invité à se connecter. Je ne souhaite pas envoyer le client vers l'URL de connexion de Wordpress mais bien vers la page "Login" créée à cet effet et ensuite revenir sur la page produit.
    – Yves
    Commented Mar 31 at 15:00
  • 1
    OK, if you're using a custom login page, you're either writing your own code—in which case you should be able to pass along the product page's URL in the querystring—or you're using a plugin, in which case you should contact the plugin's support team about this. Third-party plugin support is off topic here. (Aussi: je sais parler français, mais WPSE est un site anglais. Si tu ne parles pas anglais, je recommande d'utiliser Google Translate, que vous avez utilisé, je crois, pour votre question.)
    – Pat J
    Commented Mar 31 at 16:08
  • I'm sorry, here is the translation of my previous comment. When the customer is on the product page and wishes to leave a review, He is invited to log in. I do not want to send the customer to the Wordpress connection URL but rather to the "Login" page created for this purpose and then return to the product page. I want to write my own code and I am not using a plugin in this case.
    – Yves
    Commented Mar 31 at 17:04

1 Answer 1

0

This is how I managed a similar situation - to allow logged in authors access to a topics pitch form - using these steps (I'll add my code examples below):

  1. I put the form in a Hidden Div that was visible only to logged in users, if a site visitor was not logged in, instead they see a "login or apply" message - in my case the "login" displayed the login form in an overlay (fancybox style) but you could also link it directly to the wp-login.php page and then add a bit more code to redirect them back to where they were....in my case the "apply" link leads to a Page where they can apply to register as an authorized user, but they're not automatically approved so after applying they go to a "thanks we'll be in touch" page.

  2. I created the shortcode to display only the login form, not the wp-login.php page and show it in an overlay so my site visitors didn't leave the page they were on.

Here's the code I used:

  1. Hiding a form from non-logged in users - 2 possible methods:

    a) the easy way is to use a forms plugin that has the option to hide/show a form base on logged in status, I use GravityForms and this is easy to accomplish and allows for an HTML notice (but not PHP) to show in place of the form if a user is not logged in, the "no PHP" rule is why I created a shortcode:

    b) IF you're manually creating the review form OR using a plugin that doesn't have the option to hide/show the form based on logged-in status, then simply wrap your form in some code like this:

     if ( is_user_logged_in() ) {
     display your form code or shortcode here;
     } else {
     echo 'You must be logged in to access our pitch form, please <a class="fancybox-inline" href="#fancyboxID-2">Login</a> or <a href="https://(mysite-redacted-for-privacy)/Apply">Apply</a>';
     echo '<div style="display:none;"><div id="fancyboxID-2">';
     echo do_shortcode('[displayLogin]');
     echo '</div></div>';
     }
    

Note the "echo do_shortcode" - that's the shortcode below to display just the login form, not the login page. If someone is logged in, they'll see your form....if not, they'll see a link to "login" or "apply", if they click on login, it will show the login form in an overlay, fancybox style (I use Easy FancyBox plugin).

  1. Shortcode to display login form in the overlay:

    add_shortcode( 'displayLogin', 'display_loginForm' );
    function display_loginForm() {
    $args = array(
    'echo'           => false,
    'value_remember'       => true,);
    $loginForm = wp_login_form($args);
    
    $html = '';
    $html.= '<div id="popupLogin" class="bordered rounded">';
    $html.= $loginForm;
    $html.='</div>';
    $html.='<span class="popupLoginFPWD"><a href="https://(mysite-redacted-for-privacy)/wp-login.php?action=lostpassword" target="_self">Lost Your Password?</a></span>';
    
    return $html;
    }
    

NOTE: I default the "remember me" checkbox to true because all my authors complained about being logged out, so I added code to extend their login duration to a year if they check the box, you may not want to do that, just change the value for "value_remember" to false if you want the normal 2-day login duration.

I went with this method because redirecting users after logging in can be tricky, the default WP behavior is generally to take users to the WP Admin back end with whatever menu items their role allows, for the lowest role (subscriber) it usually only gives them their own profile page.

You certainly CAN redirect them elsewhere - such as back to your reviews page - but that's not always the best user experience, especially if they didn't want to go back to that page but had decided instead to maybe edit their profile. I would give thought to all considerations of handling this - for example, you might want to create a new class of User - say "reviewer" for example - and that class never gets to the back-end. For those who already have a higher role, you might offer an interstitial page or message offering them the option of going to the reviews page OR the back end.

Good luck with this! I'll be interested in whatever final solutions you land on. :-)

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