2

I want to set a link. If behaves differently for logged and non-logged users. If non-logged user click it, it will go to a login page. And if logged users click it, it will go to woocommerce my account page. Suggest me right code please.

<?php

case 'login':
    if ( ! function_exists( 'WC' ) ) {
        break;
    }
    $toggle = is_user_logged_in() ? 'link' : sober_get_option('account_icon_behaviour');
    printf('<li class="menu-item menu-item-account"><a href="%s" data-toggle="%s" data-target="login-modal"><svg viewBox="0 0 20 20"><use xlink:href="#user-account-people"></use></svg></a></li>',
            esc_url( wc_get_account_endpoint_url( 'dashboard' ) ),esc_attr( $toggle ));
    break;
  • you just need to make a link to woocommerce "my account". if the customer is not logged in, this page show a login form. – Kaperto Nov 24 '19 at 19:00
  • Please edit your question and copy-paste the code as a text, because as image we cannot reproduce your code – Adriana Hernández Nov 24 '19 at 19:44
  • case 'login': if ( ! function_exists( 'WC' ) ) { break; } $toggle = is_user_logged_in() ? 'link' : sober_get_option( 'account_icon_behaviour' ); printf( '<li class="menu-item menu-item-account"><a href="%s" data-toggle="%s" data-target="login-modal"><svg viewBox="0 0 20 20"><use xlink:href="#user-account-people"></use></svg></a></li>', esc_url( wc_get_account_endpoint_url( 'dashboard' ) ), esc_attr( $toggle ) ); break; – Asif Jalil Nov 24 '19 at 20:17
2

WordPress has a function for this, called is_user_logged_in. You use it like this:

if (is_user_logged_in())
  echo '<a href="account page">link text</a>';
else
  echo '<a href="login page">link text</a>';
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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