1

I am trying to make my block attributes show up in the REST API.

To start, I've added the rest_api_init hook to whitelist my block for inclusion.

add_action(
    'rest_api_init',
    function () {

        if ( ! function_exists( 'use_block_editor_for_post_type' ) ) {
            require ABSPATH . 'wp-admin/includes/post.php';
        }

        // add Location Block to 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 ) {
                            $raw_blocks= parse_blocks( $post['content']['raw'] );
                            $whitelisted_blocks = [];
                            foreach ($raw_blocks as $raw_block) {
                                if( $raw_block['blockName']=='myplugin/block-map-location' ){
                                    array_push($whitelisted_blocks, $raw_block);
                                }
                            }
                            return $whitelisted_blocks;
                        },
                    ]
                );
            }
        }
    }
);

This outputs my raw block content, but the attrs array is empty.

blocks: 
  0:
  blockName:    "myplugin/block-map-location"
  attrs:    []
  innerBlocks:; []
  innerHTML:    "\n<div class=\"wp-block-myplugin-block-map-location\" aria-label=\"Interactive Map\" role=\"region\"><figure><div class=\"map-pp\" id=\"placepress-map\" data-lat=\"41.50214445\" data-lon=\"-81.6751670486689\" data-zoom=\"13\" data-basemap=\"carto_voyager\"></div><figcaption class=\"map-caption-pp\">This is the map caption.</figcaption></figure></div>\n"
  innerContent: 
    0:  "\n<div class=\"wp-block-myplugin-block-map-location\" aria-label=\"Interactive Map\" role=\"region\"><figure><div class=\"map-pp\" id=\"placepress-map\" data-lat=\"41.50214445\" data-lon=\"-81.6751670486689\" data-zoom=\"13\" data-basemap=\"carto_voyager\"></div><figcaption class=\"map-caption-pp\">This is the map caption.</figcaption></figure></div>\n"

To solve this, I've tried the following, using the example from the Gutenberg Handbook, but it doesn't seem to have any affect. (Note that in this instance, I am working with a custom "locations" post type and trying to get "lat" and "lon" attributes from my block.)

add_action( 'init', 'register_block_attributes' );
function register_block_attributes() {
    register_meta( 'post', 'lat', array(
        'object_subtype' => 'locations',
        'show_in_rest' => true,
    ) );
    register_meta( 'post', 'lon', array(
        'object_subtype' => 'locations',
        'show_in_rest' => true,
    ) );
}

I'm obviously missing something but am not finding any answers in the documentation.

1 Answer 1

0

Answering my own question. Since register_meta() actually appends to meta, not blocks, in the JSON output, I just added a new (redundant) attribute to my block using source:meta where I can manually manage outside the attributes I use to display the block.

attributes: {
  api_coordinates: {
    type: 'string',
    source: 'meta',
    meta: 'api_coordinates',
  }, //...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.