2

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:

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?

1 Answer 1

6

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)
3
  • Perfect. Thanks! Do you, by chance, know what the 1st variable is actually for? Commented Jan 10, 2014 at 15:27
  • from the codex; wp_add_dashboard_widget($widget_id, $widget_name, $callback, $control_callback, $callback_args ); Commented Sep 6, 2014 at 19:13
  • $control_callback (string) (optional) The name of a function you create that will handle submission of widget options (configuration) forms, and will also display the form elements. Commented Sep 6, 2014 at 19:22

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.