WordPress.org

Support

Support » Miscellaneous » PHP code for displaying something for certain category of users only

PHP code for displaying something for certain category of users only

  • How can I use php to make something visible for certain users only? As in, making a link appear only if you’re an editor or admin?

Viewing 3 replies - 1 through 3 (of 3 total)
  • We can adapt some code of mine for this:

    global $current_user, $wp_roles;
    if( $current_user->id )  {
    	foreach($wp_roles->role_names as $role => $Role) {
    		if (array_key_exists($role, $current_user->caps))
    			break;
    	}
    }
    if( ('administrator' == $role) ||  ('editor' == $role) ) :
    ?>
    
    ~ display this if current user is an editor or admin ~
    
    <?php endif;?>

    Hopefully the WordPress devs come to their senses and realise the user role needs to be a little more available for this sort of thing…

    Thank you SO much, I’ve been trying to do this for such a long time. Even now though, however I’m having some trouble, with me being a PHP beginner and all.

    This is what I have at the moment where the code needs to be implemented:

    else:
    
    echo "<div class='greeting'>&raquo; <a href='"; echo bloginfo('wpurl');
    echo "/wp-admin/' class=greeting>Inst&auml;llningar</a>";
    echo "&raquo; <a href=http://www.doublepeace.se/forum/profile.php?id="; global $current_user;
    echo $current_user->ID;
    echo " class=greeting>";
    echo "Forumprofil</A>";
    		echo "&raquo; <a href='"; echo get_settings('siteurl') . '/wp-login.php?action=logout&redirect_to=' . $_SERVER['REQUEST_URI']; echo "' class=greeting>Logga ut</a>\n</div>\n";

    The

    &raquo; <a href='"; echo bloginfo('wpurl');
    echo "/wp-admin/' class=greeting>Inst&auml;llningar</a>";

    part is what should only show up if you’re admin or editor. If you could just shove your code in there for me I’d be very, very grateful, I’ve been trying for a while now constantly messing up 🙂 This is my slightly modified version of the Login Anywhere plugin, btw.

    Either way, thank you very much for the help so far!

    Nevermind, I did it. Thank you! 😀

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘PHP code for displaying something for certain category of users only’ is closed to new replies.