Support

Account

Home Forums Gutenberg Custom blocks inserted via Gutenberg editor returning JSON Reply To: Custom blocks inserted via Gutenberg editor returning JSON

  • Here’s my solution. While this post hasn’t even been read, at least this will be here if someone else runs into the situation.

    In a nutshell, the custom blocks that ACF can create for Gutenberg are stored as JSON in the content field which are then hidden by HTML comments.

    While this hidden-JSON content is searchable and will cause an item to appear in the search result, the content itself is “invisible” to WordPress’ core get_the_excerpt() or get_the_content() functionality which you commonly use in search results. The result is a search result with no blurb.

    The work-around I found for my project was to attempt to generate an excerpt. Then, if the excerpt was empty, try to generate a blurb by search for custom blocks.

    The following example is a bit messy, but nobody’s even looked for the past three weeks, so… if you have questions, I’ll try to help.

    
    <div class="search-blurb"><?php
    
      // Attempt to generate an excerpt
      $excerpt = get_the_excerpt();
    
      // If no excerpt is generated, try to generate an excerpt from ACF blocks
      if ( !$excerpt ) {
    
        // Grab ACF blocks  from page
        if ( function_exists( 'get_field' ) ) {
          $pid = get_post();
          if ( has_blocks( $pid_content ) ) {
            $blocks = parse_blocks( $pid->post_content );
    
            // Cycle through each block
            foreach ( $blocks as $block ) {
    
              // If the result is not empty, you have a blurb, so move on
              if ( $excerpt != '' ) {
                break;
    
              // Otherwise, $excerpt is still empty so try to fill it
              } else {
    
                /* In the following if/else block, I run through the custom blocks that I assume will have content worthy of a search result blurb */
    
                if ( $block['blockName'] === 'acf/narrow-content-block' ) {
                  // Uncomment the following section to SEE what JSON you have to work with
                  /*
                  echo 'acf/narrow-content-block<br /><pre>';
                  print_r( $block );
                  echo '</pre>';
                  */
    
                  // The $block['attrs'['data']['narrow_content_content'] property has the best chance to generate blurb-worthy content in this custom block
                  $excerpt .= $block['attrs']['data']['narrow_content_content'];
    
                } elseif ( $block['blockName'] === 'acf/image-video-block' ) {
                  /*
                  echo 'acf/image-video-block<br /><pre>';
                  print_r( $block );
                  echo '</pre>';
                  */
                  $excerpt .= $block['attrs']['data']['img_vid_text'];
    
                } elseif ( $block['blockName'] === 'acf/general-content-block' ) {
                  /*
                  echo 'acf/general-content-block<br /><pre>';
                  print_r( $block );
                  echo '</pre>';
                  */
                  $excerpt .= $block['attrs']['data']['general_content_content'];
    
                } elseif ( $block['blockName'] === 'acf/inset-cta-block' ) {
                  /*
                  echo 'acf/inset-cta-block<br /><pre>';
                  print_r( $block );
                  echo '</pre>';
                  */
                  $excerpt .= $block['attrs']['data']['inset_cta_content'];
    
                }
              }
            }
          }
        }
      }
    
      // Generate a blurb from the $excerpt you've generated
      $excerpt = substr( $excerpt, 0, 260);
      $result = substr( $excerpt, 0, strrpos( $excerpt, ' ') );
      echo ( $result ? strip_tags( $result ).'...' : '' );
    ?></div>