options – Retrieve my custom settings in the settings/ API endpoint

Question

I did read this post: Serialized settings in rest api and while I am actually convinced the answer is what I need, I cannot make it work.

What I am trying to achieve is to get my own settings retrieved when calling the /wp-json/wp/v2/settings/ endpoint.

My settings are registered as:

 public function __construct() {
      add_action( 'init', array( $this, 'coopedia_settings_init' ) );
      add_action( 'rest_api_init', array( $this, 'coopedia_settings_init' ) );
}

public function coopedia_settings_init() {
      register_setting(
        'coopedia_settings_group', // option_group
        'coopedia_settings', // option_name
        array(
          'show_in_rest' => array(
            'schema' => array(
              'type'       => 'object',
              'properties' => array(
                'identity_provider_uri' => array(
                    'type' => 'string',
                ),
                'classifications_uri' => array(
                    'type' => 'string',
                ),
              )
            )
          ),
          'sanitize_callback' => array($this, 'coopedia_settings_sanitize'),
     ),
  );
}

Updating and registering the settings actually works, they are displayed in my settings page and saved properly.

But then, when calling the endpoint inside a Gutenberg block using the following syntax:

wp.api.loadPromise.then(async () => {
      this.settings = new wp.api.models.Settings();
      if (false === this.state.isAPILoaded) {
        this.settings.fetch().then((response) => {
          // Push the coopedia settings classification website in state
          this.setState({
            classifications_uri: response.classifications_uri,
            isAPILoaded: true,
          });
        });
      }
}

The call to this.settings.fetch() is executed (I checked that on the network tab), but I only get the default ones:

{
  "title": "coopedia-test",
  "description": "Just another WordPress site",
  "url": "http://coopediatest.local",
  "email": "benoit+32@startinblox.com",
  "timezone": "",
  "date_format": "F j, Y",
  "time_format": "g:i a",
  "start_of_week": 1,
  "language": "en_US",
  "use_smilies": true,
  "default_category": 1,
  "default_post_format": "0",
  "posts_per_page": 10,
  "default_ping_status": "open",
  "default_comment_status": "open"
}

What am I missing here ? By the way the documentation is unclear regarding the possibilities to extend default endpoints, neither the usable wp.* objects in the new Gutenberg meta blocks.

0
Balessan 1 month 2021-04-08T18:10:24-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse