Skip to content

Instantly share code, notes, and snippets.

Avatar

UncatCrea (creators of WP-AppKit) uncatcrea

View GitHub Profile
View Use liveQuery to send data from the app to the server
/**
* WP-AppKit has no webservice dedicated to data update yet (this is one of the things on our roadmap).
* For now a workaround is to used the "liveQuery" webservice that was designed to retrieve custom
* content from server but can also work to update data.
*
* The example here shows how to use liveQuery to send data from the app to the server
* and handle this data on server side.
*
* The App.liveQuery() webservice used here is pretty convenient to handle any custom request from app to server.
* We haven't found time to write doc about it yet but we have hope it will come soon ;)
View How to update WordPress data from App
/**
* WP-AppKit has no webservice dedicated to data update yet (this is one of the things on our roadmap).
* The now native WP Rest API can be used to achieve that by implementing your own ajax calls to the Rest API
* that you would call manually from your WP-AppKit app.
* But if you want to stay within WP-AppKit possibilities, you can use the WP-AppKit's "liveQuery" web service
* which is designed to retrieve custom content from server but can also work to update data.
*
* The example here shows how to update a post meta from the app, but it can be transposed to update any WordPress data.
* Note: Sent data is not secured here. If you're on https, it's ok. If not, just be aware that the data sent is not encrypted,
* just like when you submit a form on a non https website.
@uncatcrea
uncatcrea / gist:5f18ebc1dcb973217d24
Created Dec 11, 2015
Modify the default number of posts displayed in a list
View gist:5f18ebc1dcb973217d24
add_filter( 'wpak_posts_list_posts_per_page', 'wpak_set_nb_posts', 10, 2 );
function wpak_set_nb_posts( $nb_items, $component ) {
//Check component slug to apply only to some of the "post list" components:
//(don't do this check to apply to all "post list" components)
if ( $component->slug === 'my-component-slug' ) {
$nb_items = 100; //Post number you want
}
return $nb_items;
View Add featured image caption to web service post data
//In your theme's php folder for example:
add_filter( 'wpak_post_data', 'wpak_add_thumbnail_caption', 10, 3 );
function wpak_add_thumbnail_caption( $post_data, $post, $component ) {
$thumbnail_id = get_post_thumbnail_id( $post->ID );
if ( $thumbnail_id ) {
$image_post = get_post( $thumbnail_id );
if ( $image_post ) {
if ( !empty( $post_data['thumbnail'] ) ) {
$post_data['thumbnail']['caption'] = $image_post->post_excerpt;
@uncatcrea
uncatcrea / WP-AppKit : custom template for a given component
Last active Oct 14, 2015
This example shows how to use a custom template ("my-component-template.html") for a given component ("my-component-slug") and how to retrieve component's data from template
View WP-AppKit : custom template for a given component
//In theme's functions.js
/**
Use a custom 'my-component-template.html' template only for the
'my-component-slug' component :
*/
App.filter( 'template', function( template, current_screen ) {
if ( current_screen.component_id == 'my-component-slug' ) {
template = 'my-component-template'; //Don't need .html here.
View WP-AppKit : Customize a component's query
<?php
// Write the following in a WP plugin of yours
// or in the php directory of your WP-AppKit theme.
/**
* For this example we use the "wpak_posts_list_query_args" hook
* to customize the query to sort the posts of our
* "my-component-slug" component by alphabetical order.
*
* Internally the component's query is a WP_Query object: see
@uncatcrea
uncatcrea / WP-AppKit : use the User Authentication API to secure a link
Last active Sep 2, 2015
The current use of the User Authentication API is to secure web services. Here is how to use it to secure a link.
View WP-AppKit : use the User Authentication API to secure a link
@uncatcrea
uncatcrea / WP-AppKit : Use the 'template' filter
Last active Oct 14, 2015
Shows how to use the 'template' filter to use a custom template for a given app screen
View WP-AppKit : Use the 'template' filter
/**
Use a custom 'archive-my-category.html' template only for the
'my-category' category post list :
*/
App.filter( 'template', function( template, current_screen ) {
if ( TemplateTags.isCategory( 'my-category', current_screen ) ) {
template = 'archive-my-category'; //Don't need .html here.
}
return template;
} );
View WP-AppKit : Create a custom page on app side that has its own custom route and template
//In app's theme (functions.js)
/**
The following allows to create a custom screen on app side only
(meaning it does not correspond to an existing WordPress page or post).
In this example, the page is accessed at the "url" #my-page-route and
uses the template 'my-page-template' to render. Last arguments allows to pass
custom data to the template.
*/
App.addCustomRoute( 'my-page-route', 'my-page-template', { some_data : 'for the template' } );
@uncatcrea
uncatcrea / Add post terms with wpak_post_data hook
Last active Aug 29, 2015
By default WP-AppKit doesn't get post terms to keep web service responses light. But thanks to the wpak_post_data, you can customize the returned data.
View Add post terms with wpak_post_data hook
/*
* Applies to: since WP-AppKit 0.1
* Goal: Use wpak_post_data to add post terms to the web service response and display them in app
You may want to look at https://codex.wordpress.org/Function_Reference/wp_get_post_terms
* Usage: WordPress: in the app's theme PHP folder or a separate plugin.
* App: single.html or archive.html
*/
add_filter( 'wpak_post_data', 'add_terms_to_my_app_posts', 10, 3 );
function add_terms_to_my_app_posts ( $post_data, $post, $component ) {