37

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


33

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 '...


28

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


25

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',...


16

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


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

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


9

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

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

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

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.


5

The WordPress way to access PHP variables with JavaScript is to use wp_localize_script(). function wpse_enqueue_scripts(){ wp_enqueue_script( 'wpse', PATH_TO . 'script.js' ); wp_localize_script( 'wpse', 'credentials', $credentials ); } add_action( 'wp_enqueue_scripts', 'wpse_enqueue_scripts' ); Then in your JavaScript, you can access the credentials ...


4

Look at your validation function (the one referenced by your register_setting call). Whatever it returns replaces the entire setting in the database. So if it's not getting the setting and then altering it and returning the entire setting, you'll lose the data that you didn't have it return.


4

Instead of using add_settings_section() and add_settings_field() every time, create a function that returns an array of options for example: function my_theme_options() { $options = array(); $options[] = array( 'id' => 'ID', 'title' => 'Title', 'type' => 'text_field', // use this value to sanitize/...


4

I know this is an old question but I'll throw my two cents in, just in case someone else comes along; a simpler way to do so would be do write you own modification of the add_settings_section() function, which would only add a callback args parameter at the end of it. This would look like this ( or something like it ) function my_add_settings_section($id, ...


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