WordPress.org

Ready to get started?Download WordPress

Forums

Logout Fuction in an echo statement? (8 posts)

  1. montana949
    Member
    Posted 5 years ago #

    Can anyone help me out? I have an if statement checking if users are logged in and then if they are showing them a link to their dashboard and logout link. I cannot get the function to work in the echo statement though! I would like the logout link to log them out & redirect to the current page and have it also say Welcome, username! instead of Welcome, friend!

    Check out my code below:

    <?php   if (is_user_logged_in()){
               echo "<h3><span class='slash'>//</span>Welcome, Friend!</h3><br /><br /><li><a href='http://blog.complimedia.com/wp-admin/'>Your Dashboard</a></li><li><a href='/wp-login.php?action=logout' title='Log out of this account'>Log out</a></li>";}
             else {
               echo "<h3><span class='slash'>//</span>Welcome, guest!</h3><br /><br /><li><a href='/wp-login.php'>Sign In Here</a></li>";
               };
    ?>

    Thanks so much!

  2. t31os
    Member
    Posted 5 years ago #

    Works for me....

  3. montana949
    Member
    Posted 5 years ago #

    Are you using wordpress 2.7.1? I get this error:

    You are attempting to log out of Complimedia Online Blog
    
    Please try again.

    How Can I add the correct logout link (<?php echo wp_logout_url(get_permalink()); ?>) inside the echo statement?

    I would also really like to check the registered users name and display it inside the echo statement but I do not know how.

    Something like this:

    if (is_user_logged_in()){
               echo "<h3>Welcome, wt_get_user_name();!</h3>
    
    <li><a href='wp_logout_url(get_permalink());' title='Log out of this account'>Log out</a></li>
    ";}
  4. t31os
    Member
    Posted 5 years ago #

    Yep on 2.7.1 .... local and remote...

    Your first piece of code functions, so if you're having issues with that it might be how you're placing your code...

    WP functions behave differently depending on what you use, some return content, some need to be echo'ed...

    I'd check the docs for any functions you want to use...

    wt_get_user_name

    I don't think that's even a WordPress function...

    Like i said, there's nothing wrong with the first piece of code you posted... it works...

  5. montana949
    Member
    Posted 5 years ago #

    The first piece of code I used does _NOT_ work. Please see this thread: http://wordpress.org/support/topic/225695?replies=8

    Basically I want to echo out wp_logout_url(get_permalink()); but I don't know how to do that in php! I've tried making that a variable and inserting the $var into the echo statement but that was unsuccessful as well.

    wt_get_user_name was found on this site: http://www.web-templates.nu/2008/08/27/get-user-name/

    once again I want to put that function in the echo statement.

  6. t31os
    Member
    Posted 5 years ago #

    So try a few tests...

    Some functions return content, others need to be echo'ed...

    a_function();

    and

    echo a_function();

    And see what comes up on the page for you...

    The most common problem i see with WordPress is people switching in and out incorrectly...

    Example::
    <?php some code <?php something else ?> blah blah blah ?>

    The original code you posted worked without a problem on my local 2.7.1 install.

    It might just be your usuage that isn't quite working, and like i said try plonking the code somewhere for testing, then again with an echo...

    Test each variable, each function and see what value they return before combining them.

  7. montana949
    Member
    Posted 5 years ago #

    Thanks t31os, I figured it out!

    I used a $var to hold the outcome of the funtions in seperate php tags. Then I echo'd the $var.

    <?php
    $logout = wp_logout_url(get_permalink());
    $username = wt_get_user_name();
    ?>
    
    <?php
      if (is_user_logged_in()){
               echo "Welcome, $username!<a href='http://blog.complimedia.com/wp-admin/'>Your Dashboard</a><a href='$logout' title='Log out of this account'>Log out</a>";
             }
             else {
               echo "Welcome, guest!";
               };
    ?>

    Also in case your wondering how I got the username I created the function with this code

    <?php function wt_get_user_name()
    {
    	global $userdata;
    	get_currentuserinfo();
    	return $userdata->user_login;
    } ?>
  8. t31os
    Member
    Posted 5 years ago #

    Initially missed this reply (WP Forum post tracking seems off - apparently i have not started a topic before :-s )...

    Anywho...

    Nice to see you got it working matey.... ;-)

Topic Closed

This topic has been closed to new replies.

About this Topic