Created
June 14, 2024 10:10
-
-
Save sarvar/30fb5867098d0a956b670423ef73f000 to your computer and use it in GitHub Desktop.
Get Image URL instead of Attachment Id in Rest API
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
function ws_register_images_field() { | |
register_rest_field( | |
'post', | |
'images', | |
array( | |
'get_callback' => 'ws_get_images_urls', | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
} | |
add_action( 'rest_api_init', 'ws_register_images_field' ); | |
function ws_get_images_urls( $object, $field_name, $request ) { | |
$medium = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), 'medium' ); | |
$medium_url = $medium['0']; | |
$large = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), 'large' ); | |
$large_url = $large['0']; | |
return array( | |
'medium' => $medium_url, | |
'large' => $large_url, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment