Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete or add Settings resolves in null response from API #8

Closed
Etheliene opened this issue Nov 24, 2022 · 2 comments
Closed

Delete or add Settings resolves in null response from API #8

Etheliene opened this issue Nov 24, 2022 · 2 comments

Comments

@Etheliene
Copy link

Describe the bug:

When alternating the default options, the API returns a NULL response.

Following scenario:

  1. Modify /src/admin/pages/advanced -> remove 'setting_5' section
  2. Modify /admin/class-wp-react-boilerplate-admin.php -> remove setting_5 in schema
  3. Modify /includes/functions.php -> remove 'setting_5' from $default_theme_optoins

Run: {yarn|npm} start / build

The response from the API is the following:

wp_react_plugin_boilerplate_options: null

Expected Behaviour:
Return the remaining or modified options

@codersantosh
Copy link
Owner

codersantosh commented Jan 7, 2023

When the setting's value is saved and later schema is changed in register_settings, WP_REST_Settings_Controller rest_validate_value_from_schema return null.

It seems that the saved options keys from the database should be in the registered schema to return the value from the WordPress settings rest API. Could you put the following code on your plugin, the issue should be fixed.

function prefix_wp_react_plugin_boilerplate_fix_schema($option_group, $option_name, $args ){

    if( $option_name === 'wp_react_plugin_boilerplate_options'){
        $my_options = get_option( $option_name, $args['default'] );
        $my_options_keys = array_keys($my_options);

        $schema_properties = $args['show_in_rest']['schema']['properties'];
        $schema_properties_keys = array_keys($schema_properties);

        $different_keys = array_diff($my_options_keys,$schema_properties_keys);
        if( $different_keys ){
            foreach ( $different_keys as $key){
                unset($my_options[$key]);
            }
            update_option( $option_name, $my_options);
        }
    }
}
add_action('register_setting','prefix_wp_react_plugin_boilerplate_fix_schema',10,3);

Note: Do not forget to change wp_react_plugin_boilerplate_options to your plugin options name.

@codersantosh
Copy link
Owner

It does not exist on version 2, with using custom API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants