How do I display user name, role and site name using HTML tags inside a dashboard notification?

Here’s what I have so far as my notification, but I can’t find any information around how to input the user’s name, role and site name within those sections. You can see where they are supposed to go. Any help would be much appreciated as I’ve been stumped for a few days now…

function my_network_notice(){
    global $pagenow;  
  if ( $pagenow == 'index.php') {
             echo '<div id="secondaryBox">
                    <div id="author">
                        <img src="/wp-content/themes/dewslyWeb/img/btn-articles-admin.png" width="40px" height="40px" />
                        <h5>[DISPLAYNAMEHERE]</h5>
                        <h6>[USERROLEHERE], [SITENAMEHERE]</h6>
                    </div>
                </div>
}
    }
    add_action('admin_notices', 'my_network_notice');

Related posts

Leave a Reply

3 comments

  1. You can use the bloginfo( 'name' ) function to display the site name. Information about the current logged in user can be retrieved using the get_currentuserinfo() function.

    Here is the fixed code:

    function my_network_notice(){
        global $pagenow, $currentuser;
        get_currentuserinfo();
        if ( $pagenow == 'index.php') : ?>
            <div id="secondaryBox">
                <div id="author">
                    <img src="<?php echo get_stylesheet_directory_uri(); ?>/img/btn-articles-admin.png" width="40px" height="40px" />
                    <h5><?php echo esc_html( $current_user->display_name ); ?></h5>
                    <h6><?php echo esc_html( translate_user_role( $current_user->roles[0] ) ); ?>, <?php bloginfo( 'name' ); ?></h6>
                </div>
            </div>
        <?php endif;
    }
    add_action( 'admin_notices', 'my_network_notice' );
    

    However, at the moment you’re just outputting the data straight to the dashboard. It would be better to create a dashboard widget (like the Right Now or Recent Drafts dashboard widgets):

    /* display the dashboard widget output */
    function wpse_77452_dashboard_widget_output() {
        global $currentuser;
        get_currentuserinfo(); ?>
        <div id="secondaryBox">
                <div id="author">
                    <img src="<?php echo get_stylesheet_directory_uri(); ?>/img/btn-articles-admin.png" width="40px" height="40px" />
                    <h5><?php echo esc_html( $current_user->display_name ); ?></h5>
                    <h6><?php echo esc_html( $current_user->roles[0] ); ?>, <?php bloginfo( 'name' ); ?></h6>
                </div>
            </div>
        <?php
    } 
    
    
    /* add the dashboard widget */
    function wpse_77452_add_dashboard_widgets() {
        wp_add_dashboard_widget(
            'network_notice', // ID of widget
            'Important Information', // title of widget
            'wpse_77452_dashboard_widget_output' // function used to display widget
        );  
    }
    
    add_action( 'wp_dashboard_setup', 'add_notice_dashboard_widgets' );
    
  2. You should wait for 'load-index.php' and not add the function to all admin_notices. The rest is simple:

    add_action( 'load-index.php', 'wpse_77448_user_notice' );
    
    function wpse_77448_user_notice()
    {
        if ( 'load-index.php' === current_filter() )
            return add_action( 'admin_notices', __FUNCTION__ );
    
        $user = wp_get_current_user();
        printf( '<pre>
    Name: %1$s
    Role: %2$s
    Site: %3$s</pre>',
            esc_html( $user->data->display_name ),
            // see http://wordpress.stackexchange.com/a/58921/73 for translation
            esc_html( translate_user_role( ucfirst( $user->roles[0] ) ) ),
            get_bloginfo( 'name' )
        );
    }
    

    Caveat: One user can have multiple roles. $user->roles[0] is a dirty hack, you should check the length of this array.

  3. First you need to get the current user data (Logged in). http://codex.wordpress.org/Function_Reference/get_currentuserinfo

    The you need to get_blogs_of_user.. I only display one now for you. If you want all you need a foreach loop for that. See in codex.

    function my_network_notice(){
        global $pagenow, $current_user; 
        get_currentuserinfo(); 
    
        $blogs = get_blogs_of_user( $current_user->ID );
    
        if ( $pagenow == 'index.php') {
            echo 
            '<div id="secondaryBox">
                <div id="author">
                    <img src="/wp-content/themes/dewslyWeb/img/btn-articles-admin.png" width="40px" height="40px" />
                    <h5>'. $current_user->display_name .'</h5>
                    <h6>'. ucfirst( $current_user->roles[0] ) .', '. $blogs[1]->blogname .'</h6>
                </div>
            </div>';
        }
    }
    add_action('admin_notices', 'my_network_notice');