41

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


39

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


29

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


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


18

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

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

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


10

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

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.


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


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


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


6

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

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

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

I dont know if it is possible to add a new field directly under the "Posts page" dropdown field, atleast I found no way to do this. But you could just add another dropdown menu to the reading settings page. This will be on the bottom thou. To do this, first we need to register our new setting and add a new settings field to the default WordPress ...


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

Fetch current options, and be sure to return an array if empty: $current_options = get_option( 'option_name', array() ); Ensure desired options are in an array: $desired_options = array( /* whatever you need goes here */ ); Merge them, using [wp_parse_args()]1: $merged_options = wp_parse_args( $current_options, $desired_options ); Update update_option(...


4

I just came across your question while trying to do a similar thing. I figured out what I needed so I thought I would post an answer here in case someone else finds it useful. In short I used this filter: https://codex.wordpress.org/Plugin_API/Filter_Reference/pre_update_option_(option_name) And it looks something like this: function ...


4

This issue is now resolved. I am posting the solution, in case someone else finds it useful. I have modified my callback for adding the checkbox list of categories so that the selected categories are saved in an array instead of separate options. It will make it much easier to find said special category and matching it with queried_object in the front end. ...


4

Where to store plugin settings fields? Options table FTW. It's cached and easy to do CRUD. Settings API or Options API? Basically, you can use Options API without Settings API but you cannot use Settings API without Options API. Even when you just need to add some fields to a existing WordPress page, you still need get_option() to retrieve data for your ...


4

I had the same issue as you did, but I found how to fix it in this tutorial: https://digwp.com/2016/05/wordpress-admin-notices/ Basically, I had my settings page outside of the Settings menu, so I had to explicitly add settings_errors() to my options page and they started working. :) Hope that helps.


4

add_action( 'admin_init', 'register_page_options' ); function register_page_options() { if (false == get_option('aicp_settings_options')) { add_option('aicp_settings_options'); } // Add Section for option fields add_settings_section( 'aicp_section', __( '....text here....', 'aicp' ), 'display_section', 'aicp_settings' ); // id, ...


4

Because the order of operations means you're passing that variable into your checked function before the printf function is even being run, which is what's parsing the enumerated variables. Here's a possible solution haven't tested it though: printf( '<input type="checkbox" id="%2$s" name="%1$s[%2$s]" %3$s)'. '<label for="%2$s">%4$s</...


4

Here's an updated version of your original code, which solves the saving issue. The reason that the content inside of the WP Editor was not saving was due to the value of the textarea_name parameter passed to wp_editor(). This is wrong: 'textarea_name' => 'pw_intro', It should look like this: 'textarea_name' => 'pw_settings[pw_intro]', I also ...


4

register_setting does not insert anything in the database. It's supposed to be run in admin_init, not just on activation, and is for building options pages with the Settings API and handles things like sanitisation and permissions when saving options in the admin. If you want to add some default option values to the database on activation, use add_option(). ...


3

While you are using Settins API, that's why you don't need to use update_option or add_option functions. Settings API would handle that by self. There were few issues that's why your code is not working. 1. add_settings_section( 'wpa_ad_insert_main', 'Ad Insertion Settings', 'wpa_ad_insert_section_text', 'wpa_ad_insert' ); 3rd parameter ...


3

Yes, it is cached. Look at the function’s source. It calls wp_load_alloptions() in background, to fetch all options, and that function adds the result to the cache: wp_cache_add( 'alloptions', $alloptions, 'options' ); If you look at the class WP_Object_Cache in wp-includes/cache.php, you will see, it is possible for every plugin to call the public method ...


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