Support

Account

Home Forums Bug Reports ACF Gutenberg blocks and JSON API do not work

Solving

ACF Gutenberg blocks and JSON API do not work

  • We are you using a lot ACF blocks in gutenberg and we found an issue with it in the WP JSON API.

    When you are creating ACF blocks and copy them to other pages the blockID is the same.

    Then we have a function for the register_rest_field to get the blocks in the REST API so we can sync them to our frontend.

    We do so like this:

    
    $blocks = parse_blocks( $post->post_content );
    
    FOR EACH BLOCKS
    
    acf_setup_meta( $block['attrs']['data'], $block['attrs']['id'], true );
    
    $fields = get_fields();
    
    acf_reset_meta( $block['attrs']['id'] );
    

    When you ask for a batch pages in the REST API like this:

    https://domain.com/wp-json/wp/v2/pages/?page=9&per_page=3

    The block field for all the pages on the URL will have the exact same ACF values as the first page.

    The bug is in acf-value-functions.php

    – function acf_get_value (rule 91-92)
    – function acf_format_value (rule 143-144)

    Here you they are posting values to the store with this code:

    `
    // Update store.
    $store->set( “$post_id:$field_name:formatted”, $value );
    `

    The problem is the $post_id for ACF blocks is the blockId which is not unique if you copy ACF blocks from page to page.

    The solution would be to make the store something like this:

    `
    // Update store.
    $store->set( “$post_id:$block_id:$field_name:formatted”, $value );
    `

    This way you have the real post_id and the block_id and the store key will be unique.

  • We are you using a lot ACF blocks in gutenberg and we found an issue with it in the WP JSON API.

    When you are creating ACF blocks and copy them to other pages the blockID is the same.

    Then we have a function for the register_rest_field to get the blocks in the REST API so we can sync them to our frontend.

    We do so like this:

    
    $blocks = parse_blocks( $post->post_content );
    
    FOR EACH BLOCKS
    
    acf_setup_meta( $block['attrs']['data'], $block['attrs']['id'], true );
    
    $fields = get_fields();
    
    acf_reset_meta( $block['attrs']['id'] );
    

    When you ask for a batch pages in the REST API like this:

    https://domain.com/wp-json/wp/v2/pages/?page=9&per_page=3

    The block field for all the pages on the URL will have the exact same ACF values as the first page.

    The bug is in acf-value-functions.php

    – function acf_get_value (rule 91-92)
    – function acf_format_value (rule 143-144)

    Here you they are posting values to the store with this code:

    `
    // Update store.
    $store->set( “$post_id:$field_name:formatted”, $value );
    `

    The problem is the $post_id for ACF blocks is the blockId which is not unique if you copy ACF blocks from page to page.

    The solution would be to make the store something like this:

    `
    // Update store.
    $store->set( “$post_id:$block_id:$field_name:formatted”, $value );
    `

    This way you have the real post_id and the block_id and the store key will be unique.

  • We are you using a lot ACF blocks in gutenberg and we found an issue with it in the WP JSON API.

    When you are creating ACF blocks and copy them to other pages the blockID is the same.

    Then we have a function for the register_rest_field to get the blocks in the REST API so we can sync them to our frontend.

    We do so like this:

    `
    $blocks = parse_blocks( $post->post_content );

    FOR EACH BLOCKS

    acf_setup_meta( $block[‘attrs’][‘data’], $block[‘attrs’][‘id’], true );

    $fields = get_fields();

    acf_reset_meta( $block[‘attrs’][‘id’] );
    `

    When you ask for a batch pages in the REST API like this:

    https://domain.com/wp-json/wp/v2/pages/?page=9&per_page=3

    The block field for all the pages on the URL will have the exact same ACF values as the first page.

    The bug is in acf-value-functions.php

    – function acf_get_value (rule 91-92)
    – function acf_format_value (rule 143-144)

    Here you they are posting values to the store with this code:

    `
    // Update store.
    $store->set( “$post_id:$field_name:formatted”, $value );
    `

    The problem is the $post_id for ACF blocks is the blockId which is not unique if you copy ACF blocks from page to page.

    The solution would be to make the store something like this:

    `
    // Update store.
    $store->set( “$post_id:$block_id:$field_name:formatted”, $value );
    `

    This way you have the real post_id and the block_id and the store key will be unique.

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

You must be logged in to reply to this topic.

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.