Created
February 1, 2015 19:44
-
-
Save klihelp/fb4f292a1ec20e97efab to your computer and use it in GitHub Desktop.
WP-API modify post response fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WP-API modify post response fields | |
* | |
*/ | |
/** | |
* Posts per page | |
*/ | |
function kli_wp_api_query_post_per_page( $query ){ | |
if ( defined( 'JSON_REQUEST' ) && TRUE == JSON_REQUEST ){ | |
$query->set( 'posts_per_page', -1); | |
} | |
} | |
add_action( 'pre_get_posts', 'kli_wp_api_query_post_per_page' ); | |
/** | |
* Modify Post Response - Remove Attachments | |
* @param object $data | |
* @return object | |
*/ | |
function kli_wp_api_json_prepare_post( $data ){ | |
/** | |
* Remove featured_image field from response | |
* | |
*/ | |
remove_filter( 'json_prepare_post', array( $wp_json_media, 'add_thumbnail_data' ), 10 , 3); | |
return $data; | |
} | |
add_action( 'json_prepare_post', 'kli_wp_api_json_prepare_post', 5); | |
/** | |
* Prepare post fields - Remove some fields | |
* @param object $data | |
* @return object | |
*/ | |
function kli_wp_api_prepare_post( $data ){ | |
// var_dump($data); | |
// exit; | |
// Remove fields | |
// !!todo - check core feature - last checked jan2015 | |
$fields = explode(',', 'status,author,guid,parent,date,format,' | |
.'_links,' | |
.'comment_status,ping_status,sticky,date_gmt,featured_image'); | |
// var_dump($fields); | |
// var_dump(each($fields)); | |
// exit; | |
foreach ($fields as $value) { | |
if ( isset($data[$value]) ) { | |
unset($data[$value]); | |
} | |
} | |
return $data; | |
} | |
add_action( 'json_prepare_post', 'kli_wp_api_prepare_post', 100); | |
/** | |
* Prepare post fields - Remove attachments fields | |
* @param object $data | |
* @return object | |
*/ | |
function kli_wp_api_prepare_attachment( $data ){ | |
// Remove fields | |
// @TODO: check core feature - updated jan2015 | |
$fields = explode(',', 'type,menu_order,' | |
.'_links,title,link,' | |
.'content,modified,slug,modified_gmt,source,is_image,'); | |
// attachment_meta fields | |
$attachment_meta_fields = explode(',', 'image_meta,' | |
.'width,height,file' | |
); | |
if (isset($fields) && count($fields)) { | |
foreach ($fields as $value) { | |
if ( isset($data[$value]) ) { | |
unset($data[$value]); | |
} | |
} | |
} | |
if (isset($attachment_meta_fields) && count($attachment_meta_fields) && isset($data['attachment_meta'])) { | |
foreach ($attachment_meta_fields as $value) { | |
if ( isset($data['attachment_meta'][$value]) ) { | |
unset($data['attachment_meta'][$value]); | |
} | |
} | |
} | |
return $data; | |
} | |
add_action( 'json_prepare_attachment', 'kli_wp_api_prepare_attachment', 100); |
API always returns meta data (links, self, author, collection, replies and version-history).
Can i filter this?
Need to do this as well.
i ve use this but nothing seems working.
i use the code but when i view page by wp-json/wp/v2/posts it showing them all. Is there anything i missing still?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perfect, but have a one problem.
API alway return meta data (links, self, author, coollection, replies and version-history).
Can i filter this?