Giter VIP home page Giter VIP logo

wpcom.js's Introduction

wpcom.js

Official JavaScript library for the WordPress.com REST API. Compatible with Node.js and web browsers.

CircleCI

How to use

Node.js

Introduce the wpcom dependency into your package.json ...

npm install --save wpcom

... and then initialize it with your API token ( [optional ] ( #authentication ) ).

// Edit a post on a site
var wpcom = require( 'wpcom' )( '<your-token>' );

wpcom
	.site( 'your-blog.wordpress.com' )
	.postsList( { number: 8 } )
		.then( list => { ... } )
		.catch( error => { ... } );

Browser

Include dist/wpcom.js.

<script src="wpcom.js"></script>
<script>
	var wpcom = WPCOM( '<your-token>' );
	var blog = wpcom.site( 'your-blog.wordpress.com' );
	blog.postsList( { number: 8 } )
		.then( list => { ... } )
		.catch( error => { ... } );
</script>

If bundling your project with webpack, you may need to add this to your main webpack.config.js file to avoid Cannot resolve module fs errors when bundling.

node: {
	fs: 'empty'
},

This instructs webpack to mock the fs module.

Authentication

Not all requests require a REST API token. For example, listing posts on a public site is something anyone can do.

If you do need a token, here are some links that will help you generate one:

API

Examples

// Edit a post on a site
var wpcom = require( 'wpcom' )( '<your-token>' );
var blog = wpcom.site( 'your-blog.wordpress.com' );
blog.post( { slug: 'a-post-slug' } ).update( data )
	.then( res => { ... } )
	.catch( err => { ... } );

You can omit the API token for operations that don't require permissions:

// List the last 8 posts on a site
var wpcom = require( 'wpcom' )();
var blog = wpcom.site( 'your-blog.wordpress.com' );
blog.postsList( { number: 8 } )
	.then( list => { ... } )
	.catch( error => { ... } );

More pre-made examples are in the examples/ directory.

Test

The token and site vars must be given to testing scripts either using TOKEN and SITE environment vars respectively or through of a config.json file into test/ folder like bellow:

{
	"site": "<site-id>",
	"token": "<token>"
}

Run tests:

$ make test-all

Also tests can be filtered using make test FILTER=<filter>:

$ make test FILTER=wpcom.site.post

License

MIT โ€“ Copyright 2014 Automattic

wpcom.js's People

Contributors

retrofox avatar tootallnate avatar dmsnell avatar timmyc avatar nylen avatar mattsherman avatar skeltoac avatar rauchg avatar rralian avatar aduth avatar bluefuton avatar chengfred avatar jkudish avatar johngodley avatar lezama avatar mjangda avatar okossa avatar rads avatar tug avatar oskosk avatar

Watchers

James Cloos avatar Gabriel Valle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.