WordPress.org

Ready to get started?Download WordPress

Forums

How to display content for specific users ? (17 posts)

  1. evo252
    Member
    Posted 3 years ago #

    Hello!

    I want to display something in my sidebar, just for specific users (determined by their ID ?) of my blog. Is it possible ?

    Thanks !

  2. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    You can try inserting this code into your sidebar.php template file:

    <?php
    if ( is_user_logged_in() ) {
      if ( $user_id = 2 ) {
        // display user_id = 2 content
      } elseif ( $user_id = 3 ) {
        // display user_id = 3 content
      } else {
        // display other user_id content
      }
    } else {
      // display content for guests
    }
    ?>
  3. evo252
    Member
    Posted 3 years ago #

    Thanks for your help,

    I've tested this code but it doesn't run... It displays the content for all authors who are logged in :/

  4. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    I quickly tested it using three different users on one of my test servers before posting ... everything seemed fine to me.

    Did you manually edit the sample above to match the specific ID of the users you want to display the different content to?

  5. evo252
    Member
    Posted 3 years ago #

    Strange, I tested that on my blog:

    <?php
    if ( is_user_logged_in() ) {
      if ( $user_id = 1 ) {
        echo '1';
      } elseif ( $user_id = 3 ) {
        echo '3';
      } else {
        echo 'online';
      }
    } else {
      echo 'not online';
    }
    ?>

    When I'm deconnected, the script displays "not online". But When I'm logged in under different users (user1 or user3 or any user), it always displays "1"...

  6. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    Yes, you are correct. The test site had other code that was providing the $user_id variable with its value. Try this:

    <?php
    $user_id = get_current_user_id();
    if ( is_user_logged_in() ) {
      if ( $user_id == 2 ) {
        // display user_id = 2 content
      } elseif ( $user_id == 3 ) {
        // display user_id = 3 content
      } else {
        // display other user_id content
      }
    } else {
      // display content for guests
    }
    ?>

    The 'is_user_logged_in()' conditional may not be absolutely necessary, its just a nice-to-do check.

  7. evo252
    Member
    Posted 3 years ago #

    Oh! It's an amazing coincidence, I've made some searches and found that I could try "$user_id ==" whereas "$user_id =". I've tried, it didn't run but I have not put the "$user_id = get_current_user_id();".

    I'm trying now and get back to you !

  8. evo252
    Member
    Posted 3 years ago #

    I've tested the function but it doesn't run :/

    It says that : Fatal error: Call to undefined function get_current_user_id() in /home/html/mysite.com/wp-content/themes/news/sidebar.php on line 20

    I've made some searches and found this function on the wordpress MU codex. Maybe this function is exclusive to MU and doesn't run on wordpress ?

  9. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    The function is found in ../wp-includes/user.php

    I have no idea why you are getting that error though.

  10. evo252
    Member
    Posted 3 years ago #

    Cais,

    I've tested it on wordpress 3.0 and your function runs perfectly !

    My curent blog is on wordpress 2.9.2, I'm uprading it now and the function would be run...!

  11. evo252
    Member
    Posted 3 years ago #

    Hi Cais,

    Just to say you that the function runs perfectly, so I want you to thank you very much for helping me !!!

    Thank you !

  12. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    You are very welcome! I'm glad it is working for you.

  13. myfoursquare
    Member
    Posted 3 years ago #

    cais,

    That is a nice piece of code!

    So, is there a way to dynamically generate your code snippet, so that it will work for a large number of user ids (using user id values from a database table).

    ie. lets say a user id 10256 was on the site. I don;t want to have to list lines of code for each of the 10000 users in your code snippet above.

    Basically, how can I serve 10000 users different content (posts) based on associated values in a database?

  14. simplistik
    Member
    Posted 3 years ago #

    can always use the members plugin: http://wordpress.org/extend/plugins/members/

  15. myfoursquare
    Member
    Posted 3 years ago #

    Thanks for the useful response Simplistik :)

    Interesting.

    Right. Maybe I could use it like this: I could generate different shortcodes dynamically and then the Members plugin would recognise those shortcodes and then feed the correct content accordingly.

    Or did you imagine using it some other way?

  16. Edward Caissie
    Theme Review Admin
    Posted 3 years ago #

    @myfoursquare - Sorry for being so late in getting back to this post ...

    ... I would likely consider something along the lines of @simplistik's idea of using a "members plugin" to accomodate a large user base. The code snippet above is great for a small handful of specific users but not so much for a potentially more generic listing of 1000's of users.

  17. barnabasnagy
    Member
    Posted 3 years ago #

    I used your second code Edward and got the following error: Parse error: syntax error, unexpected '}' in /leftsidebar.php on line 8

    my 8th line is:

    } elseif ( $user_id == 3 ) {

Topic Closed

This topic has been closed to new replies.

About this Topic