Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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)
share|improve this answer
    
Perfect. Thanks! Do you, by chance, know what the 1st variable is actually for? –  MadtownLems Jan 10 at 15:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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