Support » Plugins » Display only to certain Usernames?

  • Hi, This is my first post so please be kind.

    Is there such a plug-in or bit of code where I can display some text in a page only to certain users? It would be great to display a message to a list of users.

    Thanks Jono

Viewing 3 replies - 1 through 3 (of 3 total)
  • http://codex.wordpress.org/Function_Reference/wp_get_current_user

    then use (after you set up $current_user per the example)

    <?php
    $message_users = array('joe@schmoe.com','wendy@peterpan.org');
    if (in_array($current_user->user_email, $message_users)) {
    ?>
       <h2>Hi Folks!</h2> ... etcetera
    <?php
    }
    ?>

    Keep in mind that codex doc is incomplete, there are some defined functions that are easier to use in templates, such as is_user_logged_in() and if you’re going to use this a lot you might want to create a function such as is_user_email($email_list) and put it in your theme’s functions.php file so your templates are cleaner.

    function is_user_email( $email_list ) {
        global $current_user;
        get_currentuserinfo();
        if ( !is_array( $email_list ) ) {
            $email_list = array( $email_list );
        }
    
        return in_array( $current_user->user_email, $email_list );
    }

    This function will also handle a single email, such as
    if ( is_user_email('captain@hook.org') ) { ...

    If you prefer, you can test against user_login instead of user_email.

    Thread Starter jono73

    (@jono73)

    This is great. Thanks RogerTheriault,

    I have added the code in the functions page and added your code above in the page – it works like a dream!

    One question however, as the email addresses are in the page (in my instance in the archive.php page) – is there any way someone could see the email addresses? I would like to keep them secure. Would a bot for example see them?

    J

    Shouldn’t be an issue, as long as your server is properly configured. Try loading archive.php directly using the path (ie /wp-content/themes/foo/archive.php) and then view source. You should either get an error, or not much of any use. Only someone with login access to your machine would see them.

    If you want to make it more complex, and you know their user ids or logins, you could use that instead of user_email.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display only to certain Usernames?’ is closed to new replies.