Skip to content
Snippets Groups Projects

Modifications to WordPress REST API responses

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Erick Hitter
    eth-rest-api-mods.php 2.33 KiB
    <?php
    /*
     Plugin name: REST API Modifications
     Description: Make changes to REST API responses
     Version: 1.0
     Author: Erick Hitter
     Author URI: https://ethitter.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; version 2 of the License.
    
    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 St, Fifth Floor, Boston, MA  02110-1301  USA
    */
    
    class ETH_REST_API_Mods {
    	/**
    	 *
    	 */
    	public static function register_hooks() {
    		add_filter( 'rest_prepare_post', array( __CLASS__, 'add_medium_image' ), 10, 3 );
    		add_filter( 'sharing_show', array( __CLASS__, 'disable_sharedaddy' ) );
    	}
    
    	/**
    	 * Add a reference to a post's medium-sized image, when present
    	 *
    	 * Avoids needing to query API separately for an image
    	 */
    	public static function add_medium_image( $data, $post, $context ) {
    		// Default to no image
    		$image_url = false;
    
    		// Find an image, either from featured or attachments
    		if ( has_post_thumbnail( $post ) ) {
    			$image_url = wp_get_attachment_image_url( get_post_thumbnail_id( $post ), 'medium' );
    		} else {
    			$children = get_children( array(
    				'post_parent'    => $post->ID,
    				'post_status'    => 'inherit',
    				'post_type'      => 'attachment',
    				'order'          => 'ASC',
    				'orderby'        => 'menu_order ID',
    				'posts_per_page' => 15,
    			) );
    
    			foreach ( $children as $child ) {
    				if ( wp_attachment_is_image( $child->ID ) ) {
    					$image_url = wp_get_attachment_image_url( $child->ID, 'medium' );
    					break;
    				}
    			}
    		}
    
    		// Return modified data object
    		$data->data['medium_image'] = $image_url;
    		return $data;
    	}
    
    	/**
    	 * Don't include Jetpack's sharing stuff in REST responses
    	 */
    	public static function disable_sharedaddy( $show ) {
    		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
    			$show = false;
    
    //			add_filter( 'jetpack_sharing_display_markup', '__return_empty_string' );
    		}
    
    		return $show;
    	}
    }
    
    ETH_REST_API_Mods::register_hooks();
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment