forked from WebDevStudios/WDS-WP-REST-API-Connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
69 lines (49 loc) · 1.72 KB
/
Copy pathexample.php
File metadata and controls
69 lines (49 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?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();
// 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' );