Golang client library for WP-API (Wordpress REST API)
Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test-data
.gitignore
LICENSE
README.md
client.go
client_test.go
comments.go
comments_test.go
endpoints.md
media.go
media_test.go
meta.go
pages.go
pages_meta_test.go
pages_revisions_test.go
pages_test.go
posts.go
posts_meta_test.go
posts_revisions_test.go
posts_terms.go
posts_terms_category_test.go Updated tests to skip tests that involves post terms and categories. Mar 22, 2016
posts_terms_tag_test.go Updated tests to skip tests that involves post terms and categories. Mar 22, 2016
posts_terms_test.go
posts_test.go
revisions.go
statuses.go
statuses_test.go
taxonomies.go
taxonomies_test.go
terms.go
terms_category_test.go
terms_tag_test.go
terms_test.go
types.go
types_test.go
users.go
users_test.go
utils.go

README.md

go-wp-api

Golang client library for WP-API (Wordpress REST API)

Installation

go get github.com/sogko/go-wordpress

Usage

Quick example

package main

import (
	"github.com/sogko/go-wordpress"
	"net/http"
)

func main() {

  // create wp-api client
  client := wordpress.NewClient(&wordpress.Options{
    BaseAPIURL: API_BASE_URL, // example: `http://192.168.99.100:32777/wp-json/wp/v2`
    Username:   USER,
    Password:   PASSWORD,
  })
  	
  // for eg, to get current user (GET /users/me)
  currentUser, resp, body, _ := client.Users().Me()
  if resp.StatusCode != http.StatusOK {
    // handle error
  }
  
  // `body` will contain raw JSON body in []bytes
  
  // Or you can use your own structs (for custom endpoints, for example)
  // Below is the equivalent of `client.Posts().Get(100, nil)`
  var obj MyCustomPostStruct
  resp, body, err := client.Get("/posts/100", nil, &obj)
  // ...
  
  log.Println("Current user", currentUser)
}

For more examples, see package tests.

For list of supported/implemented endpoints, see Endpoints.md

Test

Note: Before running the tests, ensure that you have set up your test environment

Prerequisites

Setting up test environment

  • Install prequisits (see above)
  • Import ./test-data/go-wordpress.wordpress.2015-08-23.xml to your local test Wordpress installation
  • Upload at least one media to your Wordpress installation (Admin > Media > Upload)
  • Edit one (1) most recent Post to create a revision
  • Edit one (1) most recent Page to create a revision

Running test

# Set test enviroment
export WP_API_URL=http://192.168.99.100:32777/wp-json/wp/v2
export WP_USER=<user>
export WP_PASSWD=<password>

cd <path_to_package>/github.com/sogko/go-wordpress
go test

TODO

  • godoc documentation, so its easier for library users to map the REST APIs to library calls
  • Test comments API endpoint. (Currently, already implemented but not tested due to WP-API issues with creating comments reliably)
  • Support OAuth authentication