Creating an API Endpoint in WordPress

It turns out that creating an endpiont for an API in WordPress is super easy. Use cases for such a plugin include:

  • Add new posts from an external application
  • Request data from WordPress such as a feed of latest posts



query_vars['pagename'] == 'my/endpoint' ) {
require_once( __DIR__ . '/my/endpoint.php' );
}
}, 0 );

I’ve tested it in version 3.7. The parse request hook was added in WordPress 2.0 so theoretically it should work back to version 2.0.

If you have several endpoints, you may want to test $wp->query_vars['pagename'] with a regex or against an array of endpoints.

Be sure to make your endpoints secure. The plugin I created has a table of known API logins and password. The connection is initiated on the WordPress side on the plugin’s settings page and an API login and password is stored in the table to authenticate incoming requests.