0.4.1 • Published 10 years ago
wp-api-cli v0.4.1
CLI for WordPress.org REST API v2
CLI to help test-drive the WordPress.org REST API.
Installing
- WP-API v2 depends on the latest WordPress.org, so download it from Subversion.
- Install WP-API plugin from Git and activate it.
- There are some fixes that I made concerning file uploads and missing metadata, consider using the branch 'tnegri' of my fork if the fixes are not already merged.
- Install Node.js.
- Install the latest published version of this CLI on your system:
npm install --global wp-api-cli. - Update CLI from API description:
wp-api-cli update https://yoursite.com.
Authenticating with OAuth 1.0
- Install WP-API/OAuth1 from Git to support OAuth authentication, activate it.
- There are some fixes that I made concerning signature checking, consider using the branch 'tnegri' of my fork if the fixes are not already merged.
- Install WP-CLI to generate OAuth keys.
Go to your WordPress installation folder and create a new consumer key and secret for this CLI:
wp oauth1 addAuthenticate this CLI:
wp-api-cli authenticate \ --oauth_key=CONSUMER_KEY \ --oauth_secret=CONSUMER_SECRETIt will ask for an authorization token. Follow the steps on the browser, copy the authorization token and paste it on console.
- It will write the OAuth tokens to a file for further use -- this is a sensitive file, make sure to protect it!
- When you execute a command, the CLI will look for the file to grab the OAuth tokens, you may set a different file to use with the option
--oauth_file. If you want to remove the OAuth tokens from the file system:
wp-api-cli logout
Authenticating with HTTP Basic Auth
- Install WP-API/Basic-Auth from Git to support HTTP Basic Authentication, activate it.
- Use options
--http_userand--http_passwith every request you make.
Commands
After installing the CLI, you need to update its definition:
wp-api-cli update https://example.comAfter updating, ask for help to see everything available in your site:
wp-api-cli --helpExamples
Show all posts
$ wp-api-cli posts --table --project id,date,title
----------------------------------------------
| id | date | title |
----------------------------------------------
| 6 | 2015-08-22T20:27:32 | Hello, WP-API |
| 1 | 2015-08-18T23:09:35 | Hello world! |
----------------------------------------------Show a specific post
$ wp-api-cli posts --yaml --id 6 --project id,date,title,content,comment_status
id: 6
date: '2015-08-22T20:27:32'
title:
rendered: 'Hello, WP-API'
content:
rendered: |
<p>Hello, dear WP-API!</p>
comment_status: openSee docs/COMMANDS.md for a full documentation on commands.
Developers
This project is structured as follows:
index.jsis the entry point, it executes the main function defined inlib/wp-api-cli.js.lib/wp-api-cli.jswires everything up.lib/wp-api.jscontains the class that communicates with the REST APIs.lib/modules.jsloads all modules. A module is an object that provides the functionalities of the CLI.lib/modules/attachment.jshandles attaching files to requests, used to upload media.lib/modules/auth.jshandles authentication.lib/modules/bool-loader.jshandles boolean values in dynamic options.lib/modules/describe.jslet you fetch a description of the API.lib/modules/dict-loader.jshandles transforming options into objects (dict:prefix).lib/modules/file-loader.jshandles loading options from file (file:prefix).lib/modules/insecure.jsallows connection to insecure sites, e.g. with self signed certificates.lib/modules/method.jssets which HTTP verb to use in request.lib/modules/routes.jshandles all dynamic commands and options based on actual API description (plus some helpers).lib/modules/site.jslets you set which site the CLI connects to.lib/modules/text-loader.jshandlestext:prefix.lib/modules/update.jsfetches API description and writes it to a file for further use bylib/modules/routes.js.
- Files in
lib/utilsare utility functions to make coding easier.
If you want to create a new set of commands, drop a file in lib/modules and load it in modules.js. Full explanation in docs/MODULES.md.
Useful Links
- WordPress.org Subversion download
- WP-API project on GitHub
- Online documentation for WP-API v2
- WP-API/OAuth1 project on GitHub
- WP-API/Basic-Auth project on GitHub
- Node.js
- WP-CLI
Related Issues and Pull Requests
Issues
Fixes
- WP-API/WP-API: Fix file upload for attachments
- WP-API/WP-API: Fix MD5 check on file uploads
- WP-API/WP-API: Add missing 'force' arg to posts-terms DELETE
- WP-API/WP-API: Add missing 'force' argument to Users DELETE
- WP-API/WP-API: Add missing 'force' arg to Meta DELETE
- WP-API/WP-API: Fix typo in message
- WP-API/OAuth1: Fix signature check for sub-folder WP installs
- WP-API/OAuth1: Fix signature for params with special chars
- niallsmart/node-oauth-like: Fix signature with queryString
Features
- WP-API/WP-API: Add help properties to endpoint arguments
- WP-API/OAuth1: Add filter to signal error for WP-API v2
Project Overview
