I am having a page where there is a button named buy.While clicking the buy button its suppose to goto buy page if the user is logged in or to login page if the user is not logged in. I am trying to implement this using “is_user_logged_in()”. I have tried “header:location” which gives me a warning since i have written for get_header() initially. i have tried it using javascript but nothing seems to work.here is my code please help me.

<script>

function myfunction() {
    location.reload();
}    

function redirect() {
    <?php if(!is_user_logged_in()){
        ?>
  window.location.replace("login.php");
  <?php
}else{
    ?>
    window.location.replace("buy.php");
    <?php }
    ?>

}
}
</script>
<?php
get_header();
?> 

<form action="" method="post">
<div>
<br>
<input type="submit" name="delete" value="Delete" class="btn btn-primary" id="show" onclick="myfunction()">
<input type="submit" name="buy" value="Buy Now" class="btn btn-primary" onclick="redirect()">

</div>

Read more here: How to redirect to "login page" or "buy page" depending on user login status


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: