LeeF
LeeF

Reputation: 33

wordpress PHP code when logged in only

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

Upvotes: 3

Views: 92

Answers (1)

APAD1
APAD1

Reputation: 13666

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';
    };
?>

Upvotes: 5

Related Questions