File: /var/www/vcz/wp-content/plugins/volonteka-site-plugin/rest-api.php
<?php
// Adds featured_image_url and clean_excerpt to the rest api posts feed
add_action('rest_api_init', function(){
register_rest_field( array('post'),
'featured_image_url',
array(
'get_callback' => 'get_rest_featured_image_url',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field( array('post'),
'clean_excerpt',
array(
'get_callback' => 'get_rest_clean_excerpt',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field( array('post'),
'clean_title',
array(
'get_callback' => 'get_rest_clean_title',
'update_callback' => null,
'schema' => null,
)
);
});
// Returns the featured_media src. To be used in rest api posts feed
function get_rest_featured_image_url( $object, $field_name, $request ) {
if( $object['featured_media'] ){
$img = wp_get_attachment_image_src( $object['featured_media'], 'app-thumb' );
return $img[0];
}
return false;
}
// Returns the cleaned up excerpt without html tags. To be used in rest api posts feed
function get_rest_clean_excerpt( $object, $field_name, $request ) {
if( $object['excerpt'] ){
$stripped_text = wp_strip_all_tags(get_the_excerpt());
$decoded_text = html_entity_decode($stripped_text);
return $decoded_text;
}
return false;
}
// Returns the cleaned up title without html entities. To be used in rest api posts feed
function get_rest_clean_title( $object, $field_name, $request ) {
if( $object['title'] ){
return html_entity_decode(get_the_title());
}
return false;
}