How to pass arguments from add_settings_field() to the callback function?

Look at the declaration for the function:

function add_settings_field(
    $id,
    $title,
    $callback,
    $page,
    $section = 'default',
    $args    = array()
) { }

The last parameter takes your arguments and passes them to the callback function.

Example from my plugin Public Contact Data

foreach ($this->fields as $type => $desc) {
  $handle = $this->option_name . "_$type";
  $args   = array(
      'label_for' => $handle,
      'type'      => $type
  );
  $callback = array($this, 'print_input_field');

  add_settings_field(
      $handle,
      $desc,
      $callback,
      'general',
      'default',
      $args
  );
}

The function print_input_field() gets these arguments as first parameter:

/**
 * Input fields in 'wp-admin/options-general.php'
 *
 * @see    add_contact_fields()
 * @param  array $args Arguments send by add_contact_fields()
 * @return void
 */
public function print_input_field( array $args )
{
    $type   = $args['type'];
    $id     = $args['label_for'];
    $data   = get_option( $this->option_name, array() );
    $value  = $data[ $type ];

    'email' == $type and '' == $value and $value = $this->admin_mail;
    $value  = esc_attr( $value );
    $name   = $this->option_name . '[' . $type . ']';
    $desc   = $this->get_shortcode_help( $type );

    print "<input type="$type" value="$value" name="$name" id='$id'
        class="regular-text code" /> <span class="description">$desc</span>";
}

No need to touch a global variable.

Leave a Comment