Skip to content

ilago/WDS-WP-REST-API-Connect

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WDS WP REST API Connect Scrutinizer Code Quality

A tool for connecting to the JSON-based REST API for WordPress via OAuth 1.0a.

To get started, you'll need to install both the WP REST API plugin and the OAuth plugin.

Once installed and activated, you'll need to create a 'Consumer'. When you have the Consumer key and secret, you'll create a new WDS_WP_REST_API_Connect object by passing those credentials along with the REST API URL:

// Consumer credentials
$consumer = array(
	'consumer_key'    => 'YOUR CONSUMER KEY',
	'consumer_secret' => 'YOUR CONSUMER SECRET',
	'json_url'        => 'REST API URL OF SITE',
);
$api = new WDS_WP_REST_API_Connect( $consumer );

You can then use this object to retrieve the authentication request URL, or if you have been authenticated, make requests.

<?php

require_once( 'wds-wp-rest-api-connect.php' );

/**
 * Example WDS_WP_REST_API_Connect usage
 */
function wp_json_api_connect_example_test() {

	// Consumer credentials
	$consumer = array(
		'consumer_key'    => 'YOUR CONSUMER KEY',
		'consumer_secret' => 'YOUR CONSUMER SECRET',
		'json_url'        => 'REST API URL OF SITE',
	);

	$api = new WDS_WP_REST_API_Connect( $consumer );

	$auth_url = $api->get_authorization_url( array( 'test_api' => $_GET['test_api'] ) );

	// Only returns URL if not yet authenticated
	if ( $auth_url ) {
		echo '<div id="message" class="updated">';
		echo '<p><a href="'. esc_url( $auth_url ) .'" class="button">Authorize Connection</a></p>';
		echo '</div>';

		// Do not proceed
		return;
	}

	$post_id_to_view = 1;
	$response = $api->auth_get_request( 'posts/'. $post_id_to_view );

	if ( is_wp_error( $response ) ) {

		echo '<div id="message" class="error">';
		echo wpautop( $response->get_error_message() );
		echo '</div>';

	} else {

		echo '<div id="message" class="updated">';
		echo '<p><strong>'. $response['title'] .' retrieved!</strong></p>';
		echo '<xmp>auth_get_request $response: '. print_r( $response, true ) .'</xmp>';
		echo '</div>';

	}

	$post_id_to_update = 1;
	$updated_data = array( 'title' => 'Hello REST API World!' );
	$response = $api->auth_post_request( 'posts/'. $post_id_to_update, $updated_data );

	if ( is_wp_error( $response ) ) {

		echo '<div id="message" class="error">';
		echo wpautop( $response->get_error_message() );
		echo '</div>';

	} else {

		echo '<div id="message" class="updated">';
		echo '<p><strong>Post updated!</strong></p>';
		echo '<xmp>auth_post_request $response: '. print_r( $response, true ) .'</xmp>';
		echo '</div>';

	}

}
add_action( 'all_admin_notices', 'wp_json_api_connect_example_test' );

About

A tool for connecting to the JSON-based REST API for WordPress via OAuth - https://github.com/WP-API/OAuth1, https://github.com/WP-API/OAuth1

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • PHP 100.0%