How do I pass arguments to dashboard widget callback functions?

I’m struggling to use the $callback_args parameter of wp_add_dashboard_widget successfully.

The following code keeps displaying string(0)”” when dumping $args:

Read More
add_action( 'wp_dashboard_setup', 'sample_widget_setup' );

function sample_widget_setup() {

    wp_add_dashboard_widget(
        'sample_dashboard_widget',
        'Sample Widget',
        'sample_dashboard_widget_callback',
        null,
        'sample_string'
    );
}

function sample_dashboard_widget_callback($args) {
    var_dump($args);
}

How can I pass a variable to sample_dashboard_widget_callback?

Related posts

1 comment

  1. The args are stored in the 2nd variable passed to your callback function.

    add_action( 'wp_dashboard_setup', 'sample_widget_setup' );
    
    function sample_widget_setup() {
    
        wp_add_dashboard_widget(
          'sample_dashboard_widget',
          'Sample Widget',
          'sample_dashboard_widget_callback',
           null,
            'sample_string'
        );
    }
    
    function sample_dashboard_widget_callback( $var, $args ) {
        var_dump( $args );
    }
    

    Output from above:

    array
      'id' => string 'sample_dashboard_widget' (length=23)
      'title' => string 'Sample Widget' (length=13)
      'callback' => string 'sample_dashboard_widget_callback' (length=32)
      'args' => string 'sample_string' (length=13)
    

Comments are closed.