How to set a link different behavior for logged and non-logged user?

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="https://wordpress.stackexchange.com/questions/353228/%s" data-toggle="https://wordpress.stackexchange.com/questions/353228/%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;

1 Answer
1

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

if (is_user_logged_in())
  echo '<a href="https://wordpress.stackexchange.com/questions/353228/account page">link text</a>';
else
  echo '<a href="login page">link text</a>';

Leave a Comment