Home › Forums › Front-end Issues › Blocks: Returning image URL in rest API › Reply To: Blocks: Returning image URL in rest API
Hey all,
I’ve got a custom block configured via this post here:
https://www.advancedcustomfields.com/blog/acf-5-8-introducing-acf-blocks-for-gutenberg/
It works when viewing the post in Gutenberg, however, I’m setting up headless WP. I took a step further to expose the blocks API here:
https://wpscholar.com/blog/add-gutenberg-blocks-to-wp-rest-api/
This works, and I can get all of the ACF field data as JSON. The only problem is with images – I want to return it as an image URL string, but they’re being returned as image ID’s no matter what setting I adjust in ACF:
http://prntscr.com/ofxytl
I could make a custom block separate from ACF, but I’d prefer to stick with ACF since it’s what I’m using for most of my content.
The solution seems to adjust the attrs
where the custom block is being made, but I don’t know where to find that in the ACF code. Can anyone point me in the right direction?
Here’s my code:
*Functions.php (registering the custom ACF testimonial block)*
function register_acf_block_types()
{
// register a testimonial block.
acf_register_block(array(
'name' => 'testimonial',
'title' => __('Testimonial'),
'description' => __('A custom testimonial block.'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array('testimonial', 'quote'),
));
}
// Check if function exists and hook into setup.
if (function_exists('acf_register_block_type')) {
add_action('acf/init', 'register_acf_block_types');
}
*functions.php (exposing blocks API)*
function registerBlockRest()
{
if (!function_exists('use_block_editor_for_post_type')) {
require ABSPATH . 'wp-admin/includes/post.php';
}
// Surface all Gutenberg blocks in the WordPress REST API
$post_types = get_post_types_by_support(['editor']);
foreach ($post_types as $post_type) {
if (use_block_editor_for_post_type($post_type)) {
// echo "post types is: " . use_block_editor_for_post_type($post_types);
register_rest_field(
$post_type,
'blocks',
[
'get_callback' => function (array $post) {
return parse_blocks($post['content']['raw']);
},
],
);
}
}
}
if (function_exists('registerBlockRest')) {
add_action('acf/init', 'registerBlockRest');
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.