1

I'm starting to develop a new little js app that consumes a WordPress REST API. The new client app will not be on the same WordPress installation (let's assume that will be a simple JS script hosted in an web page elsewhere).

We'd love to use the "official" Backbone WP REST API client: it will facilitate a lot the development of the app.

Do you know if and haw we can install the Backbone client "outside" a WordPress installation?? where we can download it? Until now we only have used it for WordPress themes, and it comes "included" in the WordPress installation.

Some context information, if you need it:

We already have a WP Theme that interacts with the REST api endpoints, authenticating with nonces. And we have others "vanilla" js clients that authenticate with Basic Authentication over HTTPS. BUT The new client app will not be on the same WordPress installation.

We could go again with fetch() and plain "vanilla" js, but we'd love to use the Backbone client.

The problem is that we can't find a way to download it separately from WordPress and I'm neither sure it can works outside WordPress (maybe it needs other dependencies...)

Con someone point me in the right direction?

I'm also considering the use of http://wp-api.org/node-wpapi/ but I don't know if it's still maintained...

2
  • 1
    if your application is unauthenticated then those libraries won't help much as they won't do much that a basic redux store wouldn't already do. If it is going to need to make authenticated requests though then you'll face the problem of acquiring that nonce wether you use that client library or not. You could also use fetch() directly on the API and this would more than suffice for small JS apps. As for "better", asking that would take your question firmly into the realm of discussion and recommendations which would make the question offtopic
    – Tom J Nowell
    Mar 27, 2023 at 16:25
  • Thank @TomJNowell, my application is authenticated, all the REST endpoint requires authentication. The WP installation is working as headless backend of a sort of CRM. We use and continue developing it since 2014. Lot of custom post types etc. We already have a "Theme" on top of it that uses vanilla JS to interact with the rest API. Now we are proptotyping a new user interface that we want to be totally "client side" and we are trying to use the backbone client outside WP. But we aren't sure on how to do it, where to download it etc. I'll update my question with some details, than you
    – Stefano
    Mar 28, 2023 at 6:02

1 Answer 1

2

I haven't tried this directly, but I'm pretty positive you can — after all, it's a Javascript library. As long as you:

  1. Load any required dependencies;
  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.