Support

Account

Home Forums Gutenberg ACF Block: Return image url instead of ID

Solving

ACF Block: Return image url instead of ID

  • ACF Block: Return image url instead of ID

    Hello, because of client requirements, we’re using WordPress as a headless CMS with ACF. I registered gutemberg blocks with ACF and we’re returning beautiful json with “blockName”: “acf/content” and other data.

    But I have problem with Image Field – unfortunately plugin is returning only image ID, for example: “image”: 12.

    We can hit the endpoint of specific post for example: /wp-json/wp/v2/media?parent=23 to get all media images from specific page, but that causes extra responses.

    Does it possible to return somehow Image URL in blockName attrs or you have any ideas how to achieve it?

  • One update:

    I’m registering block endpoint this way:

    
    add_action(
    	'rest_api_init',
    	function () {
    
    		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 ) ) {
    				register_rest_field(
    					$post_type,
    					'blocks',
    					[
    						'get_callback' => function ( array $post ) {
    							return parse_blocks( $post['content']['raw'] );
    						},
    					]
    				);
    			}
    		}
    	}
    );

    based on tutorial: https://wpscholar.com/blog/add-gutenberg-blocks-to-wp-rest-api/

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.