0

this question was already similar asked but I don't find a way to get it working without using a plugin. So basically I got a text on my homepage which says login, so it's a normal text which i want to change to "my account" if a user is logged in. Also then the link has to change. I thought about creating 2 divs and hide one via css wether a user is logged in or not but this seems pretty inefficient and non-responsive to me. I would like to do it on my own but since I'm pretty new to php, I don't know how to do it.

Tnaks in advance

2 Answers 2

0

You can use the session functionality in PHP to determine if a user is logged in or not.

<?php
session_start();

if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
  $link = '/my-account.php';
  $text = 'My Account';
} else {
  $link = '/login.php';
  $text = 'Login';
}
?>
<a href="<?php echo $link; ?>"><?php echo $text; ?></a>
0

So, firstly thanks for your answer. The mentioned code above didn't work out for me, smh It doesn't recognize me being logged in.

Anyways I changed the if condition to the inbuilt logged_in check from wordpress/woocommerce and managed to fix it. So for everyone who is as desperate as I was, I will post the results

    <?php    
if(is_user_logged_in()) {
  $link = '/myaccount';
  $text = 'My Account';
} else {
  $link = '/login';
  $text = 'login';
}
?>
<a href="<?php echo $link; ?>"><?php echo $text; ?></a>

Use wp code snippets, create a shortcode, insert it and then style it via css :)

1
  • If it didn't work, then don't select it as the accepted answer. Instead select the one that worked. You may tick mark your own answer. That way other people will find it easily.
    – Fayaz
    Commented Feb 5, 2023 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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