Support

Account

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

  • Hi,

    I had the same issue a few month ago. My solution was similar to yours. I highlighted the search query.
    Do you found a way not to specify each block individually?
    I have a lot of blocks with individual field names and I don’t want to set every block manually.
    Here’s my code:

      $content = get_the_content();
        $blocks = parse_blocks( $content);
        foreach( $blocks as $block ) {
            $outercontent = array();
            if ($block['blockName'] == 'acf/pagehead') {
                $inner = $block['attrs']['data']['content'];
                $outercontent[] = $inner;
            } elseif ($block['blockName'] == 'acf/andsoon') {
                $inner = $block['attrs']['data']['andsoon'];
                $outercontent[] = $inner;
            }
    
            foreach ($outercontent as $innercontent){
                $innercontent = strip_tags( $innercontent );
                $keyword      = $s;
                $keyword = explode(' ',trim($keyword));
                $keyword = $keyword[0];
                $regex = "/\b" . $keyword . "\w*/i";
    
                if ( preg_match_all( $regex, $innercontent ) ) {
                    $str      = preg_replace( "/\w*$keyword\w*/i", "<b>$0</b>", $innercontent );
                    $keywordl = strlen( $keyword );
                    $keywordpos = stripos( $str, $keyword );
                    $strl       = strlen($str);
                    $str      = substr( $str, $keywordpos - 3, 130 );
                    echo '<li>...'.$str.'...</li>';
    
                }
            }
        }