How to create a dashboard widget for one user

I will start by warning you that my programming skill is limited.

I need to create a dashboard widget that will display in the WP dashboard for the users with the user_id=21.

Read More

So far I have tried the following code, but it isn’t working:

function my_catdb() {
echo '<a href="http://xxxxxxxxxxx.org/?frm_display=single-catproblem">View the Problem Problem Database.</a>';
}
/**
 * add Dashboard Widget via function wp_add_dashboard_widget()
 */
function my_catdb_setup() {
    wp_add_dashboard_widget( 'my_catdb', __( 'Cataloging Problem Database' ), 'my_catdb'         );
}
/**
 * use hook, to integrate new widget
 */
$user_id = get_current_user_id();
if ( is_user_logged_in() ) {  
if ( $user_id = 21 ) {
add_action('wp_dashboard_setup', 'my_wp_dashboard_setup');
} 
}

Any help is appreciated.
Thanks

Related posts

Leave a Reply

1 comment

  1. You have to wait for wp_loaded to use get_current_user_id().

    Example:

    add_action( 'wp_loaded', 'wpse_80061_load_dashboard_widget' );
    
    function wpse_80061_load_dashboard_widget()
    {
        if ( 1 === get_current_user_id() )
            add_action( 'wp_dashboard_setup', 'wpse_80061_add_dashboard_widget' );
            // your function:
            // add_action( 'wp_dashboard_setup', 'my_catdb_setup' );
    }
    
    function wpse_80061_add_dashboard_widget()
    {
        wp_add_dashboard_widget(
            'wpse_80061_widget',
            'Hey!',
            'wpse_80061_render_dashboard_widget'
        );
    }
    
    function wpse_80061_render_dashboard_widget()
    {
        echo 'hey!';
    }
    

    For a dashboard widget with more details see my plugin T5 Table size dashboard widget.