Roshan A P Roshan A P -4 years ago 85
Javascript Question

Conditional link in a wordpress page

I'm building a shop website, I have a button in my home page to proceed to shop page. What I want is that, the button should proceed to my-account page if the user hasn't logged in, and the shop page if he HAS logged in. How do I acheive this?

Right now, I'm adding the following inside the javascript in the page (I'm using a plugin to include the JS)

String function getlink()
{
if(is_user_logged_in())
return "/order";
else
return"/my-account";
}
document.getElementById('preorder').href = getlink();


and I'm specifying the preorder id in the
<a>
as follows:

<a id="preorder" class="bws-button bws-success bws-radius bws-plane " href="">


is there something I'm doing wrong, is there anything extra I need to do? I'm very new to html, JS and wordpress, so elaborate answers are appreciated :)

Answer Source
<?php if(is_user_logged_in()) { ?>
    <a id="preorder" class="bws-button bws-success bws-radius bws-plane "  href="<?php echo get_permalink(); ?>">button for logged in user</a>
<?php } else { ?>
    <a id="preorder" class="bws-button bws-success bws-radius bws-plane "  href="<?php echo get_permalink(); ?>">button for all</a>
<?php  } ?>

add your page id in the permalink area.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download