I'm using the Wordpress Rest API to import content from a Wordpress website into a PHP application. It's nothing complex, just a main page with the list of posts and the pages for individual posts.
I added some fields to the API response, in particular one to get the url of the first image inserted in the post.
This is the code for this part:
add_action('rest_api_init', function () {
register_rest_field('post', 'post_images', array(
'get_callback' => 'get_first_image',
'update_callback' => null,
'schema' => null
));
});
function get_first_image($obj, $name, $request)
{
$images = get_attached_media('image', $obj['id']);
$imagesArray = (array) $images;
reset($imagesArray);
$firstImageId = current($imagesArray)->ID;
$imageSrc = wp_get_attachment_image_url($firstImageId);
return $imageSrc;
}
It works fine when I'm listing posts in the main page, but from the individual post page the field is empty. The only explanation I can come up with for this is that I have this custom endpoint for the single posts:
function post_by_slug(WP_REST_Request $request)
{
$postSlug = $request->get_param('post_slug');
$lang = $request->get_param('my_lang');
$myPost = get_page_by_path($postSlug, OBJECT, 'post');
$targetPostId = apply_filters('wpml_object_id', $myPost->ID, 'post',
false, $lang);
$targetPost = get_post($targetPostId);
$postController = new \WP_REST_Posts_Controller($targetPost->post_type);
$response = $postController->prepare_item_for_response($targetPost,
$request);
return rest_ensure_response($response);
}
add_action('rest_api_init', function () {
register_rest_route('pc/v1',
"/post-slug/(?P<post_slug>\S+)/(?P<my_lang>\w+)", [
'methods' => 'GET',
'callback' => 'post_by_slug',
'args' => [
'post_slug' => 'required',
'my_lang' => 'required'
]
]);
});
From my app, I call it this way:
$client = new Client([
'base_uri' => 'http://example.com/wp-json/pc/v1/',
'headers' => [
'Content-Type' => 'application/json',
"Accept" => "application/json",
],
'verify' => false,
]);
var_dump(json_decode($client->get("post-slug/$slug/$lang")
->getBody()->getContents()));
What's strange is that accessing the same endpoint directly from the browser I can see all the fields correctly. Am I missing something ovious?