WordPress.org

Ready to get started?Download WordPress

Forums

PHP code for displaying something for certain category of users only (4 posts)

  1. airdrawndagger
    Member
    Posted 6 years ago #

    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?

  2. Kafkaesqui

    Posted 6 years ago #

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

  3. airdrawndagger
    Member
    Posted 6 years ago #

    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!

  4. airdrawndagger
    Member
    Posted 6 years ago #

    Nevermind, I did it. Thank you! :D

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.