0

I am using an onclick function to redirect the user if they are not login.

I am calling a php function in achor tag like this

<a href="#" class="fright hrtacnhor" onclick="<?php checkstatus(); ?>">save to my favorite</a>

and the function I am using is

<?php
function checkstatus(){
    wp_redirect( get_permalink( 8 ) );exit; 
}
?>

But the problem is that when i click it not call the fucntion. I also try to use wp_redirect() function directly in onclick but it not working so any one please guide me how it works. Thanks

2 Answers 2

1

You could do the redirect by means of JavaScript:

<a href="#" class="fright hrtacnhor" onclick="window.location='<?php echo get_permalink( 8 ) ?>'">save to my favorite</a>

But why yould you want to do it that way and not just use the link directly?

1
  • Because when any user click on it. Div flip and store some data related to that post. So for this purpose user have to login first.
    – Adi
    Mar 11, 2013 at 8:13
0

You can't use PHP this way.

PHP is static and will is parsed when the output is generated. When the page is viewed PHP can't be called directly anymore. Please read the documentation of PHP about this.

In this case (I guess you simply want to get the visitor on the other page) you can put the get_permalink() directly in the href property:

<a href="<?php echo get_permalink( 8 ); ?>" class="fright hrtacnhor">save to my favorite</a>

Or, if you want to use JavaScript (by the answer of t f):

<a href="#" class="fright hrtacnhor" onclick="javascript:window.location='<?php echo get_permalink( 8 ); ?>';">save to my favorite</a>

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.