Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress PHP code when logged in only

Tags:

php

wordpress

I have a custom URL generated with some PHP that is embedded into a page template.

<?php global $current_user; ?>
<?php get_currentuserinfo(); ?>

<?php echo 'http://www.mywebsite.co.uk/clients/' . $current_user->user_login . "/"; ?>

This all works great, but I'd like it so that the link is changed to a static link when the user isn't logged in, as otherwise the get_currentuserinfo part breaks and the link shown is invalid.

I've been trying to use if( is_user_logged_in() ) but it doesn't seem to like the PHP used in the link. I can get it to work if the link is static text, but not with all the PHP in there too.

My question is, how do I get these two pieces of PHP "logic" to work together so i can say... when a user is logged in, run this PHP to generate the above link, when they're not, show a different link?

Thanks in advance Lee

like image 542
LeeF Avatar asked Mar 22 '26 05:03

LeeF


1 Answers

You can use the is_user_logged_in conditional as you mentioned:

<?php
    if (is_user_logged_in()){
        echo 'http://www.mywebsite.co.uk/clients/' . $current_user->user_login . '/';
    } else {
        echo 'http://someotherlink.com';
    };
?>
like image 130
APAD1 Avatar answered Mar 23 '26 19:03

APAD1