• Resolved javiernanni

    (@javiernanni)


    Hi there, I need to customize which dashboard widgets are shown to each user. I can’t do this based on user roles, I need to do it on a per specific user basis. E.g. user A can only see widget A. User B can only see widget B.

    I can’t find any documentation on how to do this. Is it possible? I know it’s possible to do this customization on a per-user-role basis because many plugins do that. But how about on a per-user basis?

    Both code or a plugin would be super helpful.

    Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)
  • I’m not aware of this being possible. When plugins or themes add widgets they allocate them based on role, hence why you’re able to customise based on this. But per user… I’m not aware of that capability.

    Thread Starter javiernanni

    (@javiernanni)

    Hi Dartiss, thanks a ton for your quick reply. I was trying this alternative approach, but the code isn’t working for me. I thought perhaps that’s because it’s 4 years old and part of it got deprecated…

    Any idea if that could be the case?

    Here’s the link to what I’m talking about:

    https://wordpress.stackexchange.com/questions/51591/how-to-make-a-custom-dashboard-widget-to-display-custom-notification-from-admin

    Moderator bcworkz

    (@bcworkz)

    You’re right, the SE code needs some updating. get_field() should be get_user_meta(), and the parameters are in different order, and the user ID is an integer, not ‘user_1’

    Anywhere you see update_usermeta() or similar should be update_user_meta() now.

    This all assumes still that the proper values are stored in the usermeta table. I assume this can be done as described, but adjustments there may be required as well.

    @javiernanni: Glad you found a solution. Your original question was wanting a general customisation of dashboard widgets per user, rather than per the link, which is why I was struggling for a solution!

    Thread Starter javiernanni

    (@javiernanni)

    @bcworkz thanks for the help! I tried to apply some of your suggestions, but I’m lost about what the correct order should be, and that comment about the user ID being an integer. (I’m not that technical).

    Would you be so kind to take this code I have now and show me how it should look? I would appreciate that so so much!

    ———

    /**
     * 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_user_meta( '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_user_meta( 'user_message_text', 'user_' . $current_user->ID );
        echo $the_msg;
    }

    Moderator bcworkz

    (@bcworkz)

    You need to adjust the parameters for the new get_post_meta(). Here’s the corrected lines, you should be able to figure out where they go:

    $show_msg = get_user_meta( $current_user->ID, 'user_message_enable', true );
    //more code...
    $the_msg = get_user_meta( $current_user->ID, 'user_message_text', true );

    I forgot to mention that you need to add a third parameter true, otherwise the function returns an array instead of the expected string. This corrects the outdated get_field(), but don’t be surprised if there’s other issues.

    Thread Starter javiernanni

    (@javiernanni)

    Woohoooo!!! That made it work ??

    Thanks a ton man, I owe you a beer or sth

    Moderator bcworkz

    (@bcworkz)

    I’m glad it was that easy! I’ll imagine you bought my next beer, after all, it’s the thought that counts.

    If could set the status of the thread to “resolved”, that would be appreciated ??

    Thread Starter javiernanni

    (@javiernanni)

    Setting it as resolved. Just in case someone else lands here and wants to implement this, here’s how my final code looks like:

    /**
     * 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_user_meta( $current_user->ID, 'user_message_enable', true );
        $widget_title = 'Acceso Rápido a Sesiones - ' . $current_user->data->display_name;
        if( $show_msg )
    	add_meta_box( 'wpse_user_personal_message', $widget_title, 'wpse_51591_wp_dashboard_per_user', 'dashboard', 'side', 'high' );   // the 'side' statement places the widget on the right column
    }
    
    /**
     * 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_user_meta( $current_user->ID, 'user_message_text', true );
        echo $the_msg;
    }
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Customize dashboard widgets for individual users’ is closed to new replies.