How to Make a Custom Dashboard Widget to Display Custom Notification from Admin?

This can be solved using custom meta fields in the user profile (only visible to administrators) and then making an individual dashboard widget containing this meta (only visible if certain criteria is met).

Adding fields to the user profile page can be done:

1) manually

2) by a plugin

The previous Answer used another plugin, but the Comments thread explains the change.
Appart from being well coded and maintained, Advanced Custom Fields is incredibly handy.

2a) Setting up Custom User Fields

acf config thumbnail snapshot
click to enlarge

2b) Administrator editing an user

/wp-admin/user-edit.php?user_id=7
acf user editing

2c) Non-admin users viewing the Dashboard

The following will display (or not) each user a personalized message.

/**
 * Add Dashboard Widget
 */
add_action('wp_dashboard_setup', 'wpse_51591_wp_dashboard_setup');


/**
 * Only builds the Widget if the display message checkbox is enabled in the user editing screen 
 */
function wpse_51591_wp_dashboard_setup() 
{
    global $current_user;
    get_currentuserinfo();
    $show_msg = get_field( 'user_message_enable', 'user_' . $current_user->ID );
    $widget_title="Personal Messages to " . $current_user->data->display_name;
    if( $show_msg )
        wp_add_dashboard_widget( 'wpse_user_personal_message', $widget_title, 'wpse_51591_wp_dashboard_per_user' );
}

/**
 * Content of Dashboard Widget
 * Shows the content of each user's custom message
 */
function wpse_51591_wp_dashboard_per_user() 
{
    global $current_user;
    get_currentuserinfo();
    $the_msg = get_field( 'user_message_text', 'user_' . $current_user->ID );
    echo $the_msg;
}

Results in:
enter image description here


Bonus code
Adding a bit of UX into ACF: show/hide the Message Textbox if the Enable Message Checkbox is checked or not.
It’s better to change the order of the fields in ACF config, first checkbox then textbox.

add_action( 'admin_head-user-edit.php', 'wpse_51591_acf_profile' );
add_action( 'admin_head-user-new.php', 'wpse_51591_acf_profile' );

function wpse_51591_acf_profile() 
{
    ?>
    <script type="text/javascript">
    jQuery(document).ready( function($) 
    {       
        /* Wait 1.4s for ACF to be ready */
        setTimeout(function() {
            // find our checkbox
            var the_check = $('#acf-user_message_enable').find('input:last');
            
            // default state
            if( $(the_check).is(':checked') )
                $('#acf-user_message_text').fadeIn();
            else
                $('#acf-user_message_text').fadeOut();
                
            // live changes
            $(the_check).change(function () 
            {   
                if( $(this).is(':checked') ) 
                    $('#acf-user_message_text').fadeIn();
                else
                    $('#acf-user_message_text').fadeOut();
            });
        }, 1400);
        
        });
    </script>
    <?php
}

Leave a Comment