1) I’ve created custom Gutenberg blocks using Advanced Custom Fields, using the acf_register_block() function.
if ( function_exists( 'acf_register_block' ) ) {
acf_register_block( array(
'name' => 'accordion-module',
'title' => 'Accordion Module',
'description' => 'Create a series of expandable sections.',
'render_callback' => 'acf_block_render_callback',
'category' => 'client-name',
'icon' => 'editor-justify',
'keywords' => array( 'client', 'name', 'accordion' ),
) );
}
2) To my surprise, the default WordPress search is returning this content, BUT it’s returning something I can’t figure-out how to work with. This is the code I’m using in the search results page loop:
<div class="blurb"><?php
$excerpt = get_the_excerpt();
if ( $excerpt ) {
$excerpt = substr( $excerpt, 0, 260);
$result = substr( $excerpt, 0, strrpos( $excerpt, ' ') );
echo $result.'...';
} else {
echo get_the_content();
}
?></div>
If the search result is in an ACF block, get_the_excerpt()
returns nothing. If I return the_content()
, naturally the entire page is returned, but it’s formatted. When I do THIS, using get_the_content()
, it appears to do a better job returning the field content but it’s full of carriage returns and the tags are escaped. This is JSON. What should I do in this situation?
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>
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>';
}
}
}
The topic ‘Custom blocks inserted via Gutenberg editor returning JSON’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.