Support » Fixing WordPress » How to display content for specific users ?

  • Hello!

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

    Thanks !

Viewing 15 replies - 1 through 15 (of 16 total)
  • Edward Caissie

    (@cais)

    Theme Review Admin, The JellyBeen Man

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

    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 :/

    Edward Caissie

    (@cais)

    Theme Review Admin, The JellyBeen Man

    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?

    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”…

    Edward Caissie

    (@cais)

    Theme Review Admin, The JellyBeen Man

    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.

    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 !

    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 ?

    Edward Caissie

    (@cais)

    Theme Review Admin, The JellyBeen Man

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

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

    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…!

    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 !

    Edward Caissie

    (@cais)

    Theme Review Admin, The JellyBeen Man

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

    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?

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

    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?

    Edward Caissie

    (@cais)

    Theme Review Admin, The JellyBeen Man

    @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.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘How to display content for specific users ?’ is closed to new replies.