WP-API / client-cli

65 stars 20 forks source link

WP API + WP-CLI

Interact with a WordPress site remotely using WP API and WP-CLI.

Requirements

On the server:

On the client:

Setting Up

Step 0: Downloading

After you've cloned this repository down, you'll need to make sure you install the dependencies:

$ composer install

(If you're using WP-CLI's copy of Composer, use ~/.wp-cli/composer.phar install instead.)

Step 1: Creating a Consumer

Once you have WP API and the OAuth server plugins activated on your server, you'll need to create a "consumer". This is an identifier for the application, and includes a "key" and "secret", both needed to link to your site.

To create the consumer, run the following on your server:

$ wp oauth1 add

ID: 4
Key: sDc51JgH2mFu
Secret: LnUdIsyhPFnURkatekRIAUfYV7nmP4iF3AVxkS5PRHPXxgOW

Note the key and secret returned here. You'll need those in a moment.

Step 2: Linking the Client

Time to link the client to your site. These should always be run from the same directory this readme is in, unless you have the client installed globally.

Replace http://example.com/ with the site you're running WP API on. You can specify any URL on your site and the API will be automatically discovered from there. However, you should try and keep this URL the same across all commands, as it will help WP CLI run faster.

To link the client, run the following on the client:

$ wp --require=client.php api oauth1 connect http://example.com/ --key=sDc51JgH2mFu --secret=LnUdIsyhPFnURkatekRIAUfYV7nmP4iF3AVxkS5PRHPXxgOW
Open in your browser: http://example.com/oauth1/authorize?oauth_token=xCvteGTWqgYjPdQrCU1bXDv9
Enter the verification code:

Open this up in your browser, log in if needed, and authorize the account. You'll then see an authorization code: Authorization code

Copy and paste this into your terminal, hit enter and you're good to go:

$ wp --require=client.php api oauth1 connect http://example.com/ --key=sDc51JgH2mFu --secret=LnUdIsyhPFnURkatekRIAUfYV7nmP4iF3AVxkS5PRHPXxgOW
Open in your browser: http://example.com/oauth1/authorize?oauth_token=xCvteGTWqgYjPdQrCU1bXDv9
Enter the verification code: BEBMyxKTEKOXCsNqS9Q0r8pC
Authorized!
Key: kJPiCdhE8kIcIUCFGta1oNLE
Secret: FzcSbdsIC0Amuw2ZQuaAQCxVUxQ9X5qytWBbXQF7QzUADnr0

Your client is now linked to your site and account! The key and secret will be displayed if you need them, however WP-CLI will remember your credentials automatically.

Step 3: Doing the Fun Stuff

Time to mess around with the CLI commands! All commands start with wp api, and take the site you want to interact with as the first parameter.

$ wp --require=client.php api user list http://example.com/
+----+----------+----------+-------------------+---------------+
| ID | username | name     | email             | roles         |
+----+----------+----------+-------------------+---------------+
| 1  | admin    | admin    | admin@example.com | administrator |
| 2  | testuser | testuser | test@example.com  | subscriber    |
+----+----------+----------+-------------------+---------------+

$ wp --require=client.php api user get-current http://example.com/
+----+----------+-------+-------------------+---------------+
| ID | username | name  | email             | roles         |
+----+----------+-------+-------------------+---------------+
| 1  | admin    | admin | admin@example.com | administrator |
+----+----------+-------+-------------------+---------------+