adding an options menu that allows saving multiple sets of said options

here an example of how to manage multiple set of options. it needs to be completed by a nonce to avoid CSRF attack and it needs also a little bit of layout. add_action(“admin_menu”, function () { add_menu_page( “Multioptions” , “Multioptions” , “manage_options” // capability to be allowed to edit options , “MY_PLUGIN__multioptions” , function () … Read more

Trouble with WordPress Settings API: Form Submits When Fields Called Directly, Fails When Using Callbacks

Thank you for the responses Tom! You got me on track, I should have checked the rendered html earlier. Here’s the answer. The rendered html was showing hidden inputs, for clarity, I’ll just repeat my previous form with the hidden inputs that were being rendered: <form method=”post” action=”<?php echo admin_url(‘admin-post.php’); ?>”> <label>This form wont work</label> … Read more

What is the real purpose of the parameter $option_group in the function register_setting()?

The $option_group parameter of register_setting should match the $option_group parameter of settings_fields, which is typically used as part of creating a custom settings page. When you use register settings with register_setting(), your registered settings are added to a global variable of allowed options, where they are grouped by the $option_group parameter. When you submit your … Read more

How to sanitize settings API value

Add a third parameter, which is an $args array, and add your sanitization callback in there: register_setting( ‘sports_api_key’, ‘sports_api_key’, array( ‘sanitization_callback’ => ‘sanitize_text_field’ ) ); This is enough for your use case, the sanitize_text_field function already exists, so you don’t need to create it. More information here for how to add a sanitization callback and … Read more

Ignore “empty” fields when saving

First you need to catch the input then you can save it from $_POST So i assume your input fields names are myplugin_id , myplugin_api_key and myplugin_checkbox what you are currently doing is just going to replace the option with myplugin_checkbox You can try function register_myplugin_settings() { if ( isset( $_POST[‘myplugin_id’] ) && ! empty( … Read more

Do I need to validate the nonce when using the settings api?

As mentioned I wasn’t able to find it explicitly mentioned, although it was implied in some articles, that it was being done. When using the settings_fields( string $option_group ) wordpress function you can see from the source code that it includes a nonce field: https://developer.wordpress.org/reference/functions/settings_fields/ function settings_fields( $option_group ) { echo “<input type=”hidden” name=”option_page” value=”” … Read more

how to test for options(fields) in settings API?

Before saving the options the first time get_option( ‘rtwhc_settings’ ) returns false as the option doesn’t exist. Saving values, deleting them and then saving again causes the value of get_option( ‘rtwhc_settings’ ) to be an array with the field names as keys, which have empty values. You can see this, if you var_dump() the option … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)