Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Events Calendar to WP-API for AppPresser apps
<?php
/*
Plugin Name: Add Events Calendar to AppPresser
Description: This plugin adds the Events Calendar CPT and some post meta data to the WP-API.
Version: 0.1
Author: Scott Bolinger
Author URI: https://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
}
add_filter( 'register_post_type_args', 'app_tribe_type_args', 10, 2 );
function app_tribe_type_args( $args, $post_type ) {
if ( 'tribe_events' === $post_type ) {
$args['show_in_rest'] = true;
// Optionally customize the rest_base or rest_controller_class
$args['rest_base'] = 'tribe-events';
$args['rest_controller_class'] = 'WP_REST_Posts_Controller';
}
return $args;
}
add_action( 'rest_api_init', 'sb_register_post_meta' );
function sb_register_post_meta() {
register_rest_field( 'tribe_events', // any post type registered with API
'_EventCost', // this needs to match meta key
array(
'get_callback' => 'sb_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 sb_get_meta( $object, $field_name, $request ) {
return get_post_meta( $object[ 'id' ], $field_name, true );
}
add_action( 'rest_api_init', 'sb_register_template_hook' );
function sb_register_template_hook() {
register_rest_field( 'tribe_events', // any post type registered with API
'appp',
array(
'get_callback' => 'sb_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 sb_get_hook_data( $object, $field_name, $request ) {
$data = [];
// Add a featured image to the single post view in the app
$data['post_list']['below_content'] = '<div class="sb-event-price">$' . get_post_meta( $object['id'], '_EventCost', 1 ) . '</div><div class="sb-event-date">' . get_post_meta( $object['id'], '_EventStartDate', 1 ) . '</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.