A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.
33

Short answer: your name attribute values must use the schema option_name[array_key]. So, when you use … <input name="option_name[key1]"> <input name="option_name[key2]"> … you get an array as option value in your validation function: array ( 'key1' => 'some value', 'key2' => 'some other value' ) PHP does that for you, this is ...


27

Well the second bit of code is technically the correct way to do it. However, at the end of the add_settings_field() you can pass arguements. Please view the WordPress Add_Settings_Field function reference. This will help you in getting the best understanding of how the add_settings_field() function really works. Now, with that said, you could use a '...


25

When in doubt, look at the source code. Digging in to get_option(), you'll see (abbreviated): $value = wp_cache_get( $option, 'options' ); if ( false === $value ) { $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); // Has to be get_row instead of get_var because ...


24

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 ) {...


21

The premise of the question is flawed. The Customizer API is not an options API, but rather an options preview API. The Customizer API relies on either the Settings API or the Theme Mods API to register controls for existing settings added via either of the two APIs. The Customizer does not - and cannot - define/register new settings that have not already ...


21

"Error: Options Page Not Found" Bug This is a known issue in the WP Settings API. There was a ticket opened years ago, and it was marked as solved -- but the bug persists in the latest versions of WordPress. This is what the (now removed) Codex page said about this: The "Error: options page not found." problem (including a solution and explanation): ...


18

Use the filter update_option_{$option}. It runs after a successful saving. $option is the name of your option, and you get the old and the new value as parameters. From wp-includes/option.php: do_action( "update_option_{$option}", $oldvalue, $_newvalue ); Use it like this for an option wpse_themesettings: add_action( 'update_option_wpse_themesettings',...


14

You need a second form with admin_url('admin-post.php') as form action. Then you can hook into admin_post_custom_action to execute your action. Sample code: add_action( 'admin_post_wpse_79898', 'wpse_79898_test' ); function wpse_79898_test() { if ( isset ( $_GET['test'] ) ) echo esc_html( $_GET['test'] ); die( __FUNCTION__ ); } In your ...


14

Although I don't agree with your purpose, here the action hooks you may use (you have not shown us what you are using to save the options, so I can not say which one is better). If you use add_option to save options: add_option_{option_name}: Runs after a the option with name "option_name" has been added using add_option() function. For example, for the ...


13

Here is how I do it, beware, post is extensive. /* Add Menus -----------------------------------------------------------------*/ add_action('admin_menu', 'ch_essentials_admin'); function ch_essentials_admin() { /* Base Menu */ add_menu_page( 'Essentials Theme', 'Essentials Theme', 'manage_options', 'ch-essentials-options', '...


11

What you need to do is build your own data validation function. Ozh wrote a great tutorial about this earlier, but here's the gist of it ... Assume your options array is called $my_options and it contains three fields, 'text', 'age', and 'isauthorized'. You would still register it the same way: register_setting( 'my_setting', 'my_options', '...


11

For network option pages the correct form action URL is: wp-admin/network/edit.php?action=your_option_name Then you have to register a callback: add_action( 'network_admin_edit_your_option_name', 'your_save_network_options_function' ); In that callback function inspect the $_POST data, prepare the values, then save them: update_site_option( $...


10

WordPress will not do any data sanitization for you. It does do sanitization/validation of the default options. You have to pass in the third argument of register_setting and either role your own validation callback or or use one of the builtins. If your options is only going to contain a string, you could do something like this, for instance. <?php ...


8

You need to add a name attribute to your editors. The name attribute needs to be set to the value of of your option, so like this: function us_partners_cb() { $us_partners_desc = get_option( 'us_partners_desc' ); echo wp_editor( $us_partners_desc, 'uspartnersdesc', array('textarea_name' => 'us_partners_desc') ); }


8

Have a look at: The Complete Guide To The WordPress Settings API (Part 8: Validation, Sanitisation, and Input II): add_settings_field( 'Checkbox Element', 'Checkbox Element', 'sandbox_checkbox_element_callback', 'sandbox_theme_input_examples', 'input_examples_section' ); function sandbox_checkbox_element_callback() { $...


8

It depends on how you are going to use the stored data. If you want to run complex queries against the values, use a custom table with indexes optimized for those queries. If you will always just fetch all the values for a given object, create a non-public custom post type, and store the data as post meta. Do not store the data in a serialized string, ...


8

Try this. Please change the field name with yours <?php class MySettingsPage { /** * Holds the values to be used in the fields callbacks */ private $options; /** * Start up */ public function __construct() { add_action( 'admin_menu', array( $this, 'add_plugin_page' ) ); add_action( 'admin_init', ...


7

You're absolutely right that you can pass reusable form field markup to add_settings_field(). The trick is to define the data type for each setting, and then pass the same callback to each call to add_settings_field(). Within that callback, you simply add a switch that includes cases for each data type. Here's how I do it in Oenology: First, I dynamically ...


7

The page content for custom backgrounds is created in wp-admin/custom-background.php. There is no action available where the fields are printed. To add new fields we have to print JavaScript into the page footer. The action is 'admin_footer-appearance_page_custom-background'. To save those field values we have to hook into 'load-appearance_page_custom-...


7

You have to call add_settings_section() first, pass a unique ID and assign it to the page (poor name) discussion: add_settings_section( 'ads_id', 'Extra Settings', 'ads_description', 'discussion' ); Then register a callback to save your field(s) … // Register a callback register_setting( 'discussion', 'ads', 'trim' ); … and then register one ...


7

The culprit is: <?php settings_errors(); ?> It isn't needed and will produce a second "Settings saved." notification if there are no errors. Edited for formatting.


6

There are no hooks to modify the output of do_settings_sections(). Hence the only option you have is to write custom versions of the functions do_settings_sections() and do_settings_fields(). They are located in /wp-admin/includes/template.php, lines 1159-1174 and 1190-1207 (wordpress version 3.2.1), respectively (do not modify them there). You could (not ...


6

That's because your options are stored as a serialised array, in one row with name XX_theme_settings. To update one option, you would still need to retrieve the existing settings, ammend the appropriate value and update all options in your array together. For example: $my_options= get_option('XX_theme_settings');//retrieve all options $my_options['...


6

If there is problem only with html markup you should check example from official jQuery UI site here: http://jqueryui.com/tabs/ It looks like yours, but for work you must check if your site include stuff like jquery and jquery ui script, jquery css is important too. After that you should have script with: $( "#tabs" ).tabs(); And everything should work ...


6

The option will be stored in the table $wp_prefix . "options" as an array. You can get the option with $myoptions = get_option('plugin_options'); try var_export( $myoptions ); to see that. There is also a hidden page in wp-admin/options.php where you can see all saved options.


6

No, it is not. Use esc_html__( 'string', 'text_domain' ) instead (two underscores). Translated strings are unknown input. Unknown input is per default malicious. You don’t know where the language file comes from. It doesn’t even have to be the one you provided, because the path can be filtered or changed with a symlink. Even if it is your file: do you have ...


5

If you look at do_settings_fields() (line 1125, /wp-admin/includes/template.php), you can see that the table information is hardcoded into the the settings API. If you wish to not use the table, you will need to develop your own settings functions.


5

As simple as things can sometimes be: The Settings API is not the Theme Customizer. Both are different things for different tasks. Settings API You're either writing a plugin or have a theme that doesn't have options that won't need any visual feedback? Go with this option. Theme Customizer You need to have options that have a visual impact that the user ...


5

I just found this post while looking for the same issue. The solution is much simpler than it looks because the documentation is misleading : in register_setting() the first argument named $option_group is your page slug, not the section in which you want to display the setting. In the code above you should use // Update Settings ...


5

settings_fields('bp_settings_group'); bp_settings_group should be changed to bp-settings-group to match. settings_fields( $option_group ) $option_group should match the group name used in register_setting() i.e bp-settings-group. check on codex here.


Only top voted, non community-wiki answers of a minimum length are eligible