There is such a code:

// Заголовок add_settings_field( 'title_element', 'Заголовок', 'title_callback', 'about_options', 'about_section' ); // Подзаголовок add_settings_field( 'subtitle_element', 'Подзаголовок', 'subtitle_callback', 'about_options', 'about_section' ); 

After it comes this code

 // Заголовок function title_callback() { echo '<input type="text" id="title_element" name="about_options[title_element]" value="' . get_option( 'about_options' )[ 'title_element' ] . '" />'; } // Подзаголовок function subtitle_callback() { echo '<input type="text" id="subtitle_element" name="about_options[subtitle_element]" value="' . get_option( 'about_options' )[ 'subtitle_element' ] . '" />'; } 

It can be seen that title_callback and subtitle_callback are the same, only the id is different, I want the function to accept the id as a variable, but I don’t know how to do it, I tried this:

 // Подзаголовок add_settings_field( 'subtitle_element', 'Подзаголовок', 'subtitle_callback("id")', 'about_options', 'about_section' ); 

Does not work! How to transfer a variable there?

  • 2
    Possible duplicate question: How to insert the value of a variable inside the line? - Darth
  • @Darth This is not a duplicate question. subtitle_callback is the name of the callback function. No arguments of this function so not to push. The code will not work. And as it should - read my answer. - KAGG Design

1 answer 1

The third parameter to add_settings_field() must be the name of the callback function, and nothing else. No tricks with the addition of arguments to this line will not work.

To pass additional arguments to the callback function, use the fifth argument, which must be an array.

Your code should look something like this:

 add_settings_field( 'subtitle_element', 'Подзаголовок', 'subtitle_callback', 'about_options', 'about_section', array ( 'id' => $id ), ); function field_callback( $arguments ) { $id = $arguments['id']; //... } 

PS Why trying to insert an argument into the string with the name of the function does not work. Calling your function is done by such a line of code in wp-admin/includes/template.php :

 call_user_func( $field['callback'], $field['args'] ); 

The first parameter here should be the name of the callable function being called. Documentation for call_user_func () .

  • Thank you very much! - Igor