Support

Account

Home Forums Backend Issues (wp-admin) block data in backend doesn\'t include field indexes that are used in front end

Helping

block data in backend doesn\'t include field indexes that are used in front end

  • Hi, First thanks for the great plugin. I have newest PRO version in use, 5.8.7.

    I have following problem:

    I’m trying to build a block classes function that would automatically add different classes based on the block input.

    This is the funtion:

    function set_block_classes( $block_name, $block ) {
    	$classes = [ $block_name ];
    
    	if ( $block['align'] !== '' ) {
    		array_push( $classes, 'align' . $block['align'] );
    	}
    
    	if ( $block['className'] ) {
    		array_push( $classes, $block['className'] );
    	}
    
    	if ( $block['data']['use_bg_color'] === '1' ) {
    		array_push( $classes, 'has-background' );
    		array_push( $classes, 'has-theme-' . $block['data']['bg_color'] . '-background-color' );
    	}
    
    	return join( ' ', $classes );
    }

    This is working just fine in front end, but not in backend. After debugging $block in backend, this is what it is in ‘data’ index:

    'data' => 
        array (size=7)
          '_' => string 'field_5dea2d6edbc14' (length=19)
          '_use_bg_color' => string 'field_5dea2d27a16c6' (length=19)
          '_bg_color' => string 'field_5dea2cb2a16c4' (length=19)
          '_bg_image' => string 'field_5dea2cf2a16c5' (length=19)
          '_title' => string 'field_5dea2d79dbc15' (length=19)
          '_subtitle' => string 'field_5dea2d80dbc16' (length=19)
          '_text_align' => string 'field_5dea2d94dbc17' (length=19)

    So it’s only having those field_values, not the actual data. Any change to include the actual data in backend as well?

  • I use to retrieve block fields with get_field, both on the front-end and the back-end. If you experience this problem only on the back-end, try changing all instances of $block[‘data’] into $data and adding this at the beginning of the function:

    <?php
    $data = is_admin() ? get_field( 'data' ) : $block['data'] ;
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.