wordpress & a mobile app
TRANSCRIPT
& A Mobile App
Amit Sharma, 13 Llama Studio +AmitSharma13l @hypnosh
#WCMumbai
32% 26%
20%
93% 89% 87%
5% 6%
2012 2013 2014
Social Marketplace
A Social Network for Architects
Recruitment Tool for an Insurance Company
What next?
A mobile app maybe?
Is WordPress MVC?
J
M
V
C
Custom Post Type (CPT)
functions.php & Hooks
WordPress Theme HTML5 iOS app Android app
AJAX / WP REST API
AJAX / WP REST API
Creating CPTs register_post_type( ‘model’, $args );
Attributes: Custom Fields
CRUD
Create wp_insert_post( $post );
Read List
get_posts( $args );
View get_post( $id );
get_post_meta( $id, $key );
Update
wp_update_post( $post );
add_post_meta( $id, $key, $value );
update_post_meta( $id, $key, $value );
delete_post_meta( $id, $key, $value );
Delete wp_delete_post( $id );
Controller functions.php Action Hooks
Action Hooks add_action( $hook, $function );
AJAX add_action( ‘wp_ajax_theajaxhook’, ‘the_function’
); add_action( ‘wp_ajax_nopriv_theajaxhook’, ‘the_function’
);
add_action(‘wp_ajax_nopriv_myhook’, ‘myfunction’); function myfunction() {
$mydata = $_REQUEST[‘mydata’]; $result = do_something($mydata); echo json_encode($result); die();
}
$.ajax({ url: '/wp/wp-admin/admin-ajax.php', type: ‘GET', data: { action: ’myhook’, mydata: ‘some data’ })
.done(function(response) { // jQuery.parseJSON(response) is $result
});
V
C
functions.php
jQuery
Handlebar <script id=’tmpl1' type='text/x-handlebars-template'> <ul>
{{#each response.Items}} <li data-icon='false' id='event-{{ ID }}’> <span class='list-date'>{{{ e_date }}}</span> <span class='list-title'>{{ post_title }}</span><br> <span class='line-2'>{{{ post_content }}}</span> <button data-eventid="{{ ID }}"></button> </li> {{/each}}
</ul> </script>
REST API & JSON POST /posts GET /posts GET /posts/id GET /posts/id/meta POST /posts/id/meta PUT /posts/id/meta/mid DELETE /posts/id/meta/mid
Create:
List: View:
Update:
Delete:
https://github.com/WordCampMum/2015/blob/master/Amit-Sharma-WordPress-as-Backbone-for-a-Mobile-App.md