Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom API data for AppPresser 3 List Pages
<?php
/*
Plugin Name: AppPresser sample API code
Plugin URI: http://apppresser.com
Description: How to manipulate the WP-API for your app.
Version: 0.1
Author: AppPresser Team
Author URI: http://apppresser.com
License: GPLv2
*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// You can uncomment the function below if you are seeing CORS errors
// add_action('send_headers', function() {
// header("Access-Control-Allow-Origin: *");
// });
/****************************************************************************************
* Add featured image urls to post response for custom post types. This is enabled if you activate this plugin.
* Sample usage in the app files would be data.featured_image_urls.thumbnail
****************************************************************************************/
// Uncomment the line below to enable this function. Be sure to change the post type name in the callback function first
// add_action( 'rest_api_init', 'appp_add_featured_urls' );
function appp_add_featured_urls() {
register_rest_field( 'product', // change to your cpt slug. Don't need to add 'post', it's already covered
'featured_image_urls',
array(
'get_callback' => 'appp_featured_images',
'update_callback' => null,
'schema' => null,
)
);
}
function appp_featured_images( $post ) {
$featured_id = get_post_thumbnail_id( $post['id'] );
$sizes = wp_get_attachment_metadata( $featured_id );
$size_data = new stdClass();
if ( ! empty( $sizes['sizes'] ) ) {
foreach ( $sizes['sizes'] as $key => $size ) {
// Use the same method image_downsize() does
$image_src = wp_get_attachment_image_src( $featured_id, $key );
if ( ! $image_src ) {
continue;
}
$size_data->$key = $image_src[0];
}
}
return $size_data;
}
/****************************************************************************************
* Add REST API support to an already registered post type
* http://v2.wp-api.org/extending/custom-content-types/
* Access this post type at yoursite.com/wp-json/wp/v2/post_type_name
****************************************************************************************/
// Uncomment the line below to enable this function. Be sure to change the post type name in the callback function first
// add_action( 'init', 'appp_post_type_rest_support', 999 );
function appp_post_type_rest_support() {
global $wp_post_types;
//be sure to set this to the name of your post type!
$post_type_name = 'product';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
/****************************************************************************************
* Add post meta to a response
* http://v2.wp-api.org/extending/modifying/
* This example adds price to a woocommerce product. Meta key is _price, and corresponding api field will also be _price.
****************************************************************************************/
// Uncomment the line below to enable this function.
// add_action( 'rest_api_init', 'appp_register_post_meta' );
function appp_register_post_meta() {
register_rest_field( 'product', // any post type registered with API
'_price', // this needs to match meta key
array(
'get_callback' => 'appp_get_meta',
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Get the value of a meta field field
*
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request
*
* @return mixed
*/
function appp_get_meta( $object, $field_name, $request ) {
return get_post_meta( $object[ 'id' ], $field_name, true );
}
/****************************************************************************************
* How to use an AppPresser template hook
****************************************************************************************/
// Uncomment the line below to enable this function.
// add_action( 'rest_api_init', 'appp_register_template_hook' );
function appp_register_template_hook() {
register_rest_field( 'post', // any post type registered with API
'appp',
array(
'get_callback' => 'appp_get_hook_data',
'update_callback' => null,
'schema' => null,
)
);
}
/**
* Get the value of a meta field field
*
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request
*
* @return mixed
*/
function appp_get_hook_data( $object, $field_name, $request ) {
$data = [];
// Add a featured image to the single post view in the app
$data['post_detail']['above_title'] = '<div class="post-featured-wrap">' . get_the_post_thumbnail( $object['id'], 'large', array( 'class' => 'post-featured' ) ) . '</div>';
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.