WordPress Events Manager REST data

With WordPress adding a REST api, buddypress having one partially implemented it got me thinking about trying to build an app for my site, the only problem being that Events Manager plugin doesn’t expose any information via REST.

Securing access to REST

As I only allow logged in users to interact with most of my site I have added the following to functions.php.

*  Add filter to block access to REST for non logged on users
add_filter( 'rest_authentication_errors', function( $result ) {
  if ( ! empty( $result ) ) {
    return $result;
  if ( ! is_user_logged_in() ) {
    return new WP_Error( 'restx_logged_out', 'Sorry, you must be logged in to make a request.', array( 'status' => 401 ) );
return $result;

This will show ‘Sorry, you must be logged in to make a request.’ to anyone visiting the REST api without previously logging in.

Exposing Events via REST

Events Manager events are standard posts with additional fields so first we add REST API support to this post type in functions.php.

 * Add REST API support to an already registered post type.
add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
function my_post_type_args( $args, $post_type ) {
    if ( 'event' === $post_type ) {
        $args['show_in_rest'] = true;
        // Optionally customize the rest_base or rest_controller_class
        $args['rest_base']             = 'events';
        $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
    return $args;

You will now be able to visit your.site/wp-json/wp/v2/events and should see the standard post information for your events.  To expose the specific events data edit the $fields variable in the code below to include all the fields and custom attributes you wish to display.

register_meta( $object_type, '_event_start_local', $args1 );
add_action( 'init', 'register_new_meta');

function register_new_meta() {
     $args = array(
        'type' => 'string',
        'description' => '',
        'single' => true,
        'show_in_rest' => true,
	//'object_subtype' => 'event',
    $fields = ['_event_id','_event_start_time','_event_end_time','_event_start_date','_event_end_date',
    foreach ($fields as $key => $value)
        register_meta( 'post', $value, $args );   

Enable custom fields for events.

 * Enables the Excerpt meta box in Page edit screen, this allows the information
 * to appear in REST, the box on edit event,post can be blocked.
function my_add_customfields_support_for_events() {
 	add_post_type_support( 'event', 'custom-fields' );
 add_action( 'init', 'my_add_customfields_support_for_events' );

Then to hide the extra fields from event and post creation pages.

function my_remove_meta_boxes() {
	if ( ! current_user_can( 'manage_options' ) ) {
		remove_meta_box( 'postcustom', 'post', 'normal' );
                remove_meta_box( 'postcustom', 'event', 'normal' );
add_action( 'do_meta_boxes', 'my_remove_meta_boxes' );

Now when you check Events through REST you should also see the custom fields.




Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.