Skip to content

EXAMPLES

Thomas Elvin edited this page Aug 29, 2018 · 13 revisions

Code examples using GO API

The following examples use different methods using th GO API. JavaScript/jQuery is by far the easiet way of manipulating data with the API.

JavaScript + jQuery CRUD

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

    var api_url = "https://grontoverblik.dk/wp-json/wp/v2/go/";

    var auth = {
        user: "valid user/email",
        pass: "valid API key"
    }

    var website = {
    	url: "http://example.com",
    	text: "My website"
    }

    var new_data = {
    	"status": "publish",
        "title": "Test",
        "content": "This is conent",
        "go-categories": [111],
        "meta": { 
        	"_listings_level": 1,
        	"_location_id": 282,
        	"_address_line_1": "Road 123",
        	"_zip_or_postal_index": 5000,
        	"_content_field_10": "John Doe",
        	"_content_field_11": "21 23 12 12",
        	"_content_field_8": "mail@mail.com",
        	"_content_field_7": website,
        }
    }

   console.log(new_data);


      $.ajax
      ({
        type: "POST",
        url: api_url,
        dataType: "json",
        async: true,
        headers: {
          "Authorization": "Basic " + btoa(auth.user + ":" + auth.pass)
        },
        data: new_data,
        success: function (data,status){
           console.log(data);
        },
        error: function(data) {
           console.log(data);
        }
      });

});

</script>

Upload Files

Using the REST API to upload a file to WordPress is quite simple. All you need is to send the file in a POST-Request to the wp/v2/media route.

There are two ways of sending a file. The first method simply sends the file in the body of the request. The following PHP script shows the basic principle:

$file = file_get_contents( 'test.jpg' );
$url = 'http://example.com/wp-json/wp/v2/media/';
$ch = curl_init();
$username = 'admin';
$password = 'password';

curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
	'Content-Disposition: form-data; filename="example.jpg"',
	'Authorization: Basic ' . base64_encode( $username . ':' . $password ),
] );
$result = curl_exec( $ch );
curl_close( $ch );
print_r( json_decode( $result ) );

This would send the file test.jpg using CURL and create a new attachment.

You need to make sure, you are logged in as a user, who can upload media files. In our example, we are using Basic authentication. Via the headers you also have to send the Content-Disposition where you also add the filename.

The file itself is simply send in the body of the request.

The disadvantage of this way is, you can upload the file, but you can not alter the title or the caption in the same process. You would need to wait for the response and you would need to update the created media object in a second request.

Clone this wiki locally
You can’t perform that action at this time.