wp-api-console icon indicating copy to clipboard operation
wp-api-console copied to clipboard

Allow using the console with cookie authentication

Open pgk opened this issue 9 years ago • 0 comments

This is useful for local REST API development or for embedding the console as a plugin into an existing wordpress installation.

  • introduces a new auth provider
  • introduces a new api creator (core with autodiscovery)

It requires a predefined JavaScript global object is available on the page.

The name of that global is defined in config.json under globalObjectName

example config:

{
  "wordpress.org": [
    {
      "name": "localDev",
      "authType": "local_dev",
      "globalObjectName": "wpApiSettings",
      "url": "http://no-need-for-url.com"
    }
  ]
}

The JS object can be obtained like this (in this case the globalObjectName in the config should be named wpApiSettings)

wp_localize_script( 'wp-api', 'wpApiSettings', array(
  'nonce' => wp_create_nonce( 'wp_rest' ),
  'url' => esc_url_raw( site_url() ),
  'baseUrl' => esc_url_raw( trailingslashit( rest_url() ) ),
  'loggedInUserId' => get_current_user_id()
) );

pgk avatar Mar 28 '17 18:03 pgk