4

I'm trying to create a rather large and extensive settings page with various options of very similar type.

Since there will be about 20 different fields, and the differences between most of those being simply their ID, I'd like to avoid creating a separate callback for each one.

Is it possible to make a callback with a variable for the settings ID of each of these fields? That way one callback can server various settings fields of the same type.

I've tried using the $args parameter for add_setitngs_feild(), but sadly, it does not work. For example:

add_settings_field('name', 'Field Name', array($this, 'fieldCallback'), 'SettingsGrouP', 'SettingsSection', array("settingID!")); 


function fieldCallback($id)
{
    echo "<input id='" . $id . "'/>";//etc, etc
}

fieldCallback si being called, but the ID of the input is blank.

4 Answers 4

2

The last optional $args argument the you can pass to add_settings_fields() is passed to callback. So it seems you can use same callback just fine.

Hope I am right because I just stumbled onto this two minutes ago because of discussion in chat. :)

PS looked through code and it's indeed relatively recent, before ~2.9 arguments weren't passed.

6
  • Hey, thanks for the reply! I'll check this out immediately. May 19, 2011 at 18:56
  • Hmm, doesn't seem to be working, sadly :( I've posted code above in the question May 19, 2011 at 19:17
  • @Georges Oates Larsen it's passed as single array... tried var_dump($id); to check? I didn't get to playing with this today but it's highly interesting topic for me so I will definitely get to testing this later. edit I couldn't resist :) quick check and var_dump() inside callback shows that argument is passed as part of array just fine.
    – Rarst
    May 19, 2011 at 20:29
  • Changed the callback to: var_dump($id);, and it still came up blank May 20, 2011 at 16:42
  • @Georges Oates Larsen I am not sure what the reason it fails for you might be... Are you running current WP version? Also you can try simpler test case first without class and such.
    – Rarst
    May 20, 2011 at 17:13
0

In the last (optional) argument $args in add_settings_field() function you can use array('label_for' => 'something') as it is writen in documentation (see http://codex.wordpress.org/Function_Reference/add_settings_field).

So if you want to pass id of your field use 'label_for' for it :)

0

The way Wordpress passes arguments to the callback function here is a bit tricky, the callback function receives the whole $args array as parameter, so you may want to change the function as following:

function fieldCallback(array $args)
{
    echo "<input id='" . $args[0] . "'/>";//etc, etc
}

and you leave add_settings_field as it is.

-2
add_settings_field(
    'name', 
    'Field Name', 
    array($this, 'fieldCallback'), 
    'SettingsGrouP', 
    'SettingsSection',
    $args=array("settingID!")
); 

function fieldCallback($id) {
   `echo "<input id='" . $id[0] . "'/>";`
}

OR

what Jacer Omri illustrated

1
  • You should explain what the code does and why it answer the question.
    – cybmeta
    Feb 9, 2015 at 11:05

Your Answer

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

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