1

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.

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

1

1 Answer 1

0

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.

2
  • I appreciate you taking the time to help, but I'm not sure what to replace to make it work with my existing code.
    – Sayid
    Jan 10, 2013 at 16:41
  • @Sayid See my update. I have inserted a comment where you can call your function.
    – fuxia
    Jan 10, 2013 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.