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


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


19

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

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


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


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

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


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

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


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


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

Solved it by using show_user_profile and edit_user_profile instead: add_action( 'show_user_profile', 'wpse_237901_user_edit_section', 999 ); add_action( 'edit_user_profile', 'wpse_237901_user_edit_section', 999 ); function wpse_237901_user_edit_section() { # Code here... }


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


4

JSON Schema It's supported if you explicitly register the object JSON schema as: $args = array( 'show_in_rest' => array( 'schema' => array( 'type' => 'object', 'properties' => array( 'post' => array( 'type' => 'string', ), 'blog' =...


3

I've just run across this same issue myself on a project I am currently working on. Basically, what I did was break my settings into groups so they can then be displayed as a separate section(s): <?php class MyPluginSettings { /** * Constructor. * * @return void * @access public */ public function __construct() { ...


3

After you finish with following article, you will be able to add and get options from your admin with only a few lines of code. this will be creating options for a WordPress theme in this article . Titan Framework(WordPress option framework) Add new theme option function.php if (!class_exists('TitanFramework')) { require_once( ...


3

Yes, you can and it's easier using a Class. In the admin_init hook (where the Settings API is being registered and defined) set a class property based on the user ID: $this->prefix = 'uid_' . get_current_user_id() . '_'; Then, in the rest of the code, refer to your option name as $this->prefix . 'option_name'. The result in the table wp_options will ...


3

add_settings_field() accepts six arguments, the last one is an array of arguments: add_settings_field($id, $title, $callback, $page, $section, $args); You can add custom values here, so you can reuse one callback. A simple example: add_settings_field( 'foo', 'Foo', 'wpse_settingsfield_callback', 'my_page', 'my_section', array ( '...


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


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