I haven’t tried this directly, but I’m pretty positive you can — after all, it’s a Javascript library. As long as you:
- Load any required dependencies;
- Provide any settings expected by the library;
You should be good. A quick code search brings up these results from the /wp-includes/script-loader.php
file:
$scripts->add( 'wp-api', "/wp-includes/js/wp-api$suffix.js", array( 'jquery', 'backbone', 'underscore', 'wp-api-request' ), false, 1 );
So you have the main script (wp-api
) and its dependencies. wp-api-request
itself expects a settings object — here’s the relevant block from the same file:
$scripts->add( 'wp-api-request', "/wp-includes/js/api-request$suffix.js", array( 'jquery' ), false, 1 );
// `wpApiSettings` is also used by `wp-api`, which depends on this script.
did_action( 'init' ) && $scripts->localize(
'wp-api-request',
'wpApiSettings',
array(
'root' => sanitize_url( get_rest_url() ),
'nonce' => wp_installing() ? '' : wp_create_nonce( 'wp_rest' ),
'versionString' => 'wp/v2/',
)
);
So there you have it — just remember to provide a wpApiSettings
object with the required parameters.