Save button in theme customization page stays disabled

Ok here's the scenario: I have added a custom section to an existing theme so that it shows up in the theme customization page (customize.php). The problem I am having is that I don't know how to notify wordpress when changes are made in my custom control. I guess I am missing something in my code because it should do it in automatic? Here is some sample code: $wp_customize->add_section('my_section', array( 'title' => 'My title', 'description' => 'My description', 'priority' => …
Category: Web

Is it possible to get a list of options for a specific options group?

When registering options in WordPress, you are required to supply a "settings group name" along with the name of the option itself. register_setting( string $option_group, string $option_name, array $args = array() ) Given, register_setting($my_settings_group, $my_option_name1); register_setting($my_settings_group, $my_option_name2); register_setting($my_settings_group, $my_option_name3); Is it possible to then get a list of all options for the specified custom settings group? For example, something appropriate to the pseudocode... get_settings_group_options($my_settings_group); which would return either an array of option names, or option names and corresponding option values. …
Category: Web

Why aren't these WordPress options being saved to the database correctly?

I have a plugin for editing certain options already stored in the database. It creates an options page under Settings with a simple form that populates each field with whatever values are stored in the database for the given field. When the page loads, each of the options loaded into the fields are correct, but if I edit any of the fields and click "Save changes", all of the fields on the page are cleared out, in addition to deleting …
Category: Web

Callback function is being called twice

I have registered a setting, added a section in my plugin. Inside that section, multiple inputs (checkboxes) fields have been added using the add_settings_field method. This method has a callback function which I am using to return true for checkboxes which are checked and return false which are unchecked in an array. Now the problem is this function works as expected except the plugin is activated for the first time. For first time activation, the callback function is being called …
Category: Web

WordPress Settings API, Implementing Tabs On Custom Menu Page

I've been following the WordPress Settings API tutorial series of this guy: http://wp.tutsplus.com/tutorials/theme-development/the-complete-guide-to-the-wordpress-settings-api-part-1/ So far, I've been successful at doing the basic thing. Now, when implementing tab, I'm facing problem. Problem 1: The Tabs are not working. All the fields are being displayed on both the tabs. Section 1, should be displayed on Tab One and Section 2 on Tab Two. Problem 2: Option 2 is not being saved. It was working fine before implementing tabs. Code: /* ----------------------------------------------------------------------------- */ …
Category: Web

It is possible to pass $args that sent by add_settings_field() inside another function?

I'm trying To do my plugin Setting with OOP PHP when I'm Creating Setting Filed it sends $args so I can use it in their function, but when I call this function in another place it is for $args and it's undefined. here are my codes: Please Look At the Last Chapter and see the $args Add Setting: add_settings_field( 'st-test-color', __('Color', 'st-search'), 'st_color_ca', 'st-test', 'st-test-settings-section-2', array( 'name' => 'color_css', 'id' => 'st-test-color', 'class' => 'st-test-color', 'options' => $st_test, ) ); …
Category: Web

How to add a secondary button to a settings page with a custom action?

I have written a plugin that interfaces with a third-party mailing API. I have created a settings page for my plugin that includes two buttons: a submit button created with submit_button() which saves the form a "test settings" button that should use the current values from the form without saving them This is what I currently have: echo '<form method="post" action="options.php">'; settings_fields('myplugin_settings'); do_settings_sections('myplugin'); echo '<p>'; submit_button('Test Settings', '', 'test', false); echo ' '; submit_button(null, 'primary', 'submit', false); echo '</p></form>'; This …
Category: Web

How to get specific setting by settings_fields()?

I am working on a plugin and on the settings page I have 2 settings and I want to show Only one setting "backgroundColor" so how should i do that? This is the code: <form action="options.php" method="post"> <?php settings_errors(); settings_fields( 'bg_color' ); **<<< I want only background settings to show how i can do it correclty?** do_settings_sections( 'plugin-style' ); submit_button( __('Save settings', 'trans') ); ?> </form> /* Add Settings Field for Background Color */ add_settings_field( 'background-color', __('Background Color', 'trans'), 'plugin_background_color_ca', …
Category: Web

Hook if somebody saves plugin options?

I wrote a plugin that has various options and in case the user saves this options a CSS file will be generated. My intention is that the CSS is only created when the options are saved and not when somebody visits the page and the CSS will be created every single time. So I need a hook that fires when somebody saves the options. I tried it like this: public function __construct( $file ) { $this->options = get_option('clb_plugin_options'); add_filter( 'pre_update_option_clb_plugin_options', …
Category: Web

The Settings API - What is the purpose of default value in register_setting?

I'm trying to wrap my head around the Settings and Options APIs as I'm writing a simple plugin. One thing that has really stomped me is the possibility to add a default value to an option when calling the register_setting() function. This does not work the way I expected and I don't get how it's supposed to work. Here is a simplified example: <?php /** * Plugin name: Options test */ define( 'MY_PAGE', 'my_test_page' ); define( 'MY_SECTION', 'my_test_section' ); define( …
Category: Web

Displaying validation message in options-general.php

I have added a setting field in the general settings page. The field and the validation work, but the validation error message does not show. <?php add_action( 'admin_init', 'initialize_extra_settings' ); function initialize_extra_settings() { register_setting( 'general', 'setting_app_store_app_url', array ( 'type' => 'string', 'sanitize_callback' => 'settings_url_field_validation' ) ); add_settings_field( 'app-store-app-url-field', // Field slug 'App Store app URL', 'app_store_app_url_field_cb', 'general', // In this settings page (slug) 'default', // In this section (slug) array( 'label_for' => 'app-store-app-url-field', 'class' => 'custom-settings-row' ) ); } function …
Category: Web

Callback is not called in add_settings_field() when passed as part of an array, but recognises that it's there. It's passed Class to Class using OOP

I am building a plugin, I created a new admin page and subpage, and inside that subpage, I want to add some Settings. That's why I'm building them using the Settings API. I am using Composer and Object-Oriented Programming. I have a folder called SettingsAPI, with the following namespace namespace Blocks\Pages\SettingsAPI; Inside that folder, I have three files: AddSettings.php, Settings.php and SettingsLayout.php. All three of them are correctly connected. The files have the case-sensitive names of the Classes. Settings.php has …
Category: Web

How to register dynamic settings in WordPress Customizer?

I have been experimenting with the Customizer JS API to create Panels, Sections, Settings and Controls in the Customizer. Till now, I have been using the PHP API but decided to try the JS API for the first time. I figured out how to create Panels, Sections and Controls. I got stuck at Settings. Doing some research, I found out that the setting has to be either defined in PHP using $wp_customize->add_setting or by using the customize_dynamic_setting_args filter. I need …
Category: Web

Add_menu_page and saving settings

I am working on an WP plugin and have my settings in a created menu page. When I save the page my settings are saved without any problems, but I don't return to the settings page but to a page where you can see all settings that exists in WordPress. How can I return the user to my own settings page? I post to options.php as described in the help docs.
Category: Web

How to save Setting pages data to the database?

I have created a setting page and I have a basic form in there I want to save its data in the database how can I achieve that. This is the HTML form: <label class=""><?php _e('Message', 'domain'); ?> </label> <textarea class="message form-control" name="my_settings_fields[message]"><?php echo esc_html($my_setting_data[ 'message' ]); ?></textarea> This is how I registered my setting page: public function my_register_settings() { register_setting('my_setting_option_page', 'my_settings_fields', 'sd_callback_function'); } /** * Register the plugin settings page. * * @since 1.0.0 */ public function my_register_menu_page() { …
Category: Web

Can't output do_settings_sections . Can't understand why

I've this code: /* Hook to admin_menu the yasr_add_pages function above */ add_action('admin_menu', 'yasr_add_pages'); function yasr_add_pages() { //Add Settings Page add_options_page( 'Yet Another Stars Rating: Settings', //Page Title __('Yet Another Stars Rating: Settings', 'yasr'), //Menu Title 'manage_options', //capablity 'yasr_settings_page', //menu slug 'yasr_settings_page_content' //The function to be called to output the content for this page. ); } /* Settings Page Content */ function yasr_settings_page_content() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.', 'yasr')); } include(YASR_ABSOLUTE_PATH …
Category: Web

How to pass arguments from add_settings_field() to the callback function?

I have a function like this: add_settings_field( 'contact_phone', 'Contact Phone', 'settings_callback', 'general'); That works. It calls settings_callback. Cool. The problem I have with this is: I don't want to have to define a callback function for every setting I add, if all I'm doing is echoing out a little bit of stuff. function settings_callback() { echo '<input id="contact_phone" type="text" class="regular-text" name="contact_phone" />'; } Why on earth should I have to do that? The id, class, and name should all be …
Category: Web

Plugin settings page checkbox not saving - one options array with sub array

I'm driving myself crazy here trying to get a checkbox to save on a plugin settings page. Right now, as a template, I'm working through the excellent example given here: Settings API with arrays example I'm also referring to a very useful tutorial at https://www.smashingmagazine.com/2016/04/three-approaches-to-adding-configurable-fields-to-your-plugin/ with a detailed git at https://github.com/rayman813/smashing-custom-fields/blob/master/smashing-fields-approach-1/smashing-fields.php I've also read about a half dozen posts with similar questions but none have my answer, at least that I can find and understand. I think I'm stumbling on …
Category: Web

How to get bool with `get_option()` when already registered setting with type `boolean`

When I register a boolean setting using register_setting() I already pass the data type: register_setting( option_group: '…', option_name: '…', args: [ 'type' => 'boolean', 'description' => '…', 'sanitize_callback' => function (mixed $value): bool { return (bool) $value; }, 'show_in_rest' => false, 'default' => false, ], ); However, when calling the option with get_option(), it still returns me a string value: "1" I would like to compare values with === and would like to AVOID something like: (bool) get_option('…') === true …
Category: Web

Settings API not saving values to database

Here is what I'm attempting to do: I have two fields called Client ID and Client Secret. I would like to save each individual item in the database as the following keys: instagram_client_id and instagram_client_secret. Does anyone see what I'm doing wrong with the Settings API? As you can see, I am showing the fields but when I input any value, it doesn't create a database key or save any values. So I have the following items setup: private function …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.