WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How can I check if I am logged in? (7 posts)

  1. saphod
    Member
    Posted 6 years ago #

    Hi,

    I want to show some links in the sidebar of my page only if I am logged in, e.g. "New Post", "Edit Theme", "Manage" etc., so that I have direct access and do not have to go via the admin panel - saves some clicks, you know. :-)

    First, I tried

    <?php if (!$user_ID) { ?>
    (I would set up the link list here)
    <? } ?>

    That didn't work, meaning the link list would not show up at all.

    Then, I tried

    <?php if (!$user_login) { ?>
    (I would set up the link list here)
    <? } ?>

    That didn't work either, because now the list would show up even with me not being logged in.

    FORGET IT!
    I just realized the "!" = NOT in front of the variable... DAMN!

    Rechecked and edited...
    but erasing "!" does not change anything: the list would not show up...

    Any suggestions?

  2. saphod
    Member
    Posted 6 years ago #

    Hi.... anyone, please?

  3. saphod
    Member
    Posted 6 years ago #

    OK, I surrender... :(

  4. lelion
    Member
    Posted 6 years ago #

    It's a bit of code with which some PHP developer can help you... I'd search for something in the code which will be like "If user logged in, then...", then would re-use this bit of code to make your little bit of magic work :)

    Use trial and error :) I am not so proficient in PHP as to help you easily... I'm afraid :-/

  5. lelion
    Member
    Posted 6 years ago #

    PS OK, after a quick test, here's what I came up with:

    <?php
    if ( is_user_logged_in() ) {
      echo "(Put your stuff here! Like,
      (I would set up the link list here - no problems...)";
      }
    ?>

    You can modify it at your own ease, to suit your needs. I know this is not the best way to implement it, but it works. I checked, logged in and logged out, the "if" statement worked OK.

    Try it! :)

    My $ 0.02 :)

  6. saphod
    Member
    Posted 6 years ago #

    Hi lelion!

    Your code works - just the way I wanted it to work!

    The problem with my code was that I did not define $User_ID - you have to set it as a global variable and then use get_currentuserinfo(), as I have just found out.

    Anyway, your way is less complicated, and I will use that one. Thank you very much!

  7. lelion
    Member
    Posted 6 years ago #

    You're welcome!

    I'm glad my 0.01% ability to read PHP scripts helped in this case ;-)

Topic Closed

This topic has been closed to new replies.

About this Topic