Automattic / wp-api-console

WordPress (.com and .org) API Console written in React/Redux
GNU General Public License v2.0
69 stars 20 forks source link

Allow using the console with cookie authentication #75

Open pgk opened 7 years ago

pgk commented 7 years ago

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

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()
) );