Creating native apps with WordPress

42
Marko Heijnen January 14, 2012 at WordCamp Norway Creating native apps with WordPress

description

Talk at WordCamp Norway 2012 about how to create a native app with WordPress as a datasource. Going into the XML-RPC, JSON plugins and how to manage the data in the backend. The example had to be removed because of the company I worked for. Don't know why but as a result a smaller presentation.

Transcript of Creating native apps with WordPress

Page 1: Creating native apps with WordPress

Marko HeijnenJanuary 14, 2012 at WordCamp Norway

Creating native apps with WordPress

Page 2: Creating native apps with WordPress

Freelance developer

Mobile / WordPress

http://markoheijnen.com

[email protected]

@markoheijnen

Marko Heijnen

Page 3: Creating native apps with WordPress

History2006: Started with WordPress

2009: Started with iOS development

2010: First patch to WordPress ( 3.0 )

2011: Combined iOS development with WordPress

2012: First WordCamp presentation

Page 4: Creating native apps with WordPress

Lets talk mobile

Page 5: Creating native apps with WordPress

Mobile is the growing

Page 6: Creating native apps with WordPress

There is a change youget involved with it

Page 7: Creating native apps with WordPress

Native app vsWeb app/site

Page 8: Creating native apps with WordPress

Native vs mobileBuild a native app when:

People are going to use it quite often ( daily basis )

Features that aren’t possible for web ( yet )

A lot of users request for it ( 10.000 > )

Customers really want an app

Page 9: Creating native apps with WordPress

We need a native app

Page 10: Creating native apps with WordPress

What does it mean for youYou need to communicate with another developer

Need to build an API that the developer can use

So you need to understand what a mobile developer wants

Need to build additional fields in WordPress for information that the mobile app can use

Page 11: Creating native apps with WordPress

The API

Page 12: Creating native apps with WordPress
Page 13: Creating native apps with WordPress

API of the appWordPress for iOS uses XML-RPC

WordPress support it by default

Supports: Blogger API, metaWeblog API, and the Movable Type API

Page 14: Creating native apps with WordPress

Where can you find it/xmlrpc.php

handles the request

/wp-includes/class-wp-xmlrpc.server.php

Registers all the default methods

Page 15: Creating native apps with WordPress

Methods for testing/** * Test XMLRPC API by saying, "Hello!" to client. * * @since 1.5.0 * * @param array $args Method Parameters. * @return string */function sayHello($args) { return 'Hello!';}

/** * Test XMLRPC API by adding two numbers for client. * * @since 1.5.0 * * @param array $args Method Parameters. * @return int */function addTwoNumbers($args) { $number1 = $args[0]; $number2 = $args[1]; return $number1 + $number2;}

Page 16: Creating native apps with WordPress

How to get recent postfunction mw_getRecentPosts($args) {

$this->escape($args);

$blog_ID = (int) $args[0]; $username = $args[1]; $password = $args[2]; if ( isset( $args[3] ) ) $query = array( 'numberposts' => absint( $args[3] ) ); else $query = array();

if ( !$user = $this->login($username, $password) ) return $this->error;

do_action('xmlrpc_call', 'metaWeblog.getRecentPosts');

Page 17: Creating native apps with WordPress

How you can use it in your own way

Page 18: Creating native apps with WordPress

Create your own methodsadd_filter( 'xmlrpc_methods', 'add_own_methods' );

function add_own_methods( $methods ) {$methods['own.my_method'] = 'own_my_method';

return $methods}

function own_my_method( $args ) {return $some_data

}

Page 19: Creating native apps with WordPress

How does it look like

Page 20: Creating native apps with WordPress

Request<?xml version="1.0"?><methodCall><methodName>metaWeblog.getRecentPosts</methodName><params><param><value><string></string></value></param><param><value><string>username</string></value></param><param><value><string>password</string></value></param></params></methodCall>

Page 21: Creating native apps with WordPress

Response<?xml version="1.0"?><methodResponse> <params> <param> <value> <array><data> <value><struct> <member><name>dateCreated</name><value><dateTime.iso8601>20110904T18:15:26</dateTime.iso8601></value></member> <member><name>userid</name><value><string>1</string></value></member> <member><name>postid</name><value><string>1</string></value></member> <member><name>description</name><value><string>Welcome to &lt;a href=&quot;http://network.nginx.markoheijnen.com/&quot;&gt;WordPress Network Sites&lt;/a&gt;. This is your first post. Edit or delete it, then start blogging!</string></value></member> <member><name>title</name><value><string>Hello world!</string></value></member> <member><name>link</name><value><string>http://test.network.nginx.markoheijnen.com/2011/09/04/hello-world/</string></value></member> <member><name>permaLink</name><value><string>http://test.network.nginx.markoheijnen.com/2011/09/04/hello-world/</string></value></member> <member><name>categories</name><value><array><data> <value><string>Uncategorized</string></value></data></array></value></member> <member><name>mt_excerpt</name><value><string></string></value></member> <member><name>mt_text_more</name><value><string></string></value></member> <member><name>mt_allow_comments</name><value><int>1</int></value></member> <member><name>mt_allow_pings</name><value><int>1</int></value></member> <member><name>mt_keywords</name><value><string></string></value></member> <member><name>wp_slug</name><value><string>hello-world</string></value></member> <member><name>wp_password</name><value><string></string></value></member> <member><name>wp_author_id</name><value><string>1</string></value></member> <member><name>wp_author_display_name</name><value><string>marko</string></value></member> <member><name>date_created_gmt</name><value><dateTime.iso8601>20110904T18:15:26</dateTime.iso8601></value></member> <member><name>post_status</name><value><string>publish</string></value></member> <member><name>custom_fields</name><value><array><data></data></array></value></member> <member><name>wp_post_format</name><value><string>standard</string></value></member></struct></value></data></array> </value> </param> </params></methodResponse>

Page 22: Creating native apps with WordPress

XML-RPCCritics of XML-RPC argue that RPC calls can be made with plain XML

XML-RPC uses about 4 times the number of bytes compared to plain XML

Need a lot of code on the app side

In the end the size of requests/responses do mather

Page 23: Creating native apps with WordPress

Is this the best way orare there alternatives?

Page 24: Creating native apps with WordPress

AlternativesJust XML instead of XML-RPC

Use JSON

Page 25: Creating native apps with WordPress

What is JSONJavaScript Object Notation

Use JSON as alternative to XML

It is derived from the JavaScript scripting language

Lightweight text-based open standard

Page 26: Creating native apps with WordPress

Why JSONLighter and faster than XML

JSON objects are typed

Array, object, string, number, boolean and null

Great libraries for mobile platformsiOS 5 does have native support for JSON

Easy to parse on mobile platforms

Page 27: Creating native apps with WordPress

JSON Example{ "firstName" : "Marko", "lastName" : "Heijnen", "age" : 25, "address" : null, "newsletter" : false, "phoneNumber" : [ { "type" : "home", "number": "212 555-1234" }, { "type" : "fax", "number": "646 555-4567" } ] }

Page 28: Creating native apps with WordPress

WordPress doesn’t have native support (yet)

Page 30: Creating native apps with WordPress

What do I use

Page 31: Creating native apps with WordPress

JSON APIGreat to use because of the use of hooks

Good information on the plugin page

Simple backend interface

I use a modified version of it

Added consumer key/secret

Page 32: Creating native apps with WordPress

Interface

Page 33: Creating native apps with WordPress

How to implementfunction add_hello_controller( $controllers ) {

$controllers[] = 'hello';return $controllers;

}add_filter( 'json_api_controllers', 'add_hello_controller' );

function set_hello_controller_path() {return "/path/to/theme/hello.php";

}add_filter( 'json_api_hello_controller_path', 'set_hello_controller_path' );

Page 34: Creating native apps with WordPress

How to implement<?phpclass JSON_API_Hello_Controller {

public function hello_world() {return array( "message" => "Hello, world" );

}}?>

Page 35: Creating native apps with WordPress

Important to knowOnly return data that is needed

Return as less HTML as possible

Don’t change the feed without notifying the app developer

A call shouldn’t take to long, speed is everything

Cache the data if possible

Transients: http://codex.wordpress.org/Transients_API

Page 36: Creating native apps with WordPress

Recap

Page 37: Creating native apps with WordPress

How to managemobile content

Page 38: Creating native apps with WordPress

Post typesAdd separate meta box for mobile content

Even the ability to overrule the title

Additional user capability, so not everyone can manage it

Maybe even create a special post type for mobile content

Page 39: Creating native apps with WordPress

Special admin pageOnly for mobile configuration

Store settings

Store default pages for mobile like privacy disclaimer

Page 40: Creating native apps with WordPress

Push notificationsCreate ability to send push notifications in a smart way

Add to posttype

Make a separate box on dashboard or admin page

You can handle the sending yourself

Or use for example the services of Urban Airship

Page 41: Creating native apps with WordPress

Last wordWordPress is a CMS

Can be used in a lot of ways

Sometimes look further then the plugin section