Blocks
The blocks utility functions provide tools for working with Gutenberg and ACF blocks in WordPress.
Available Functions
cbo_get_first_block_name()
Retrieves the name of the first ACF block on the current page.
Return Value:
Returns the name of the first block or false if no block is found.
1// Get the first block name2$first_block = cbo_get_first_block_name();34if ($first_block) {5echo 'The first block is: ' . $first_block;6} else {7echo 'No blocks found on this page.';8}910// Use the first block name to add a specific class to the body11function add_first_block_class($classes) {12$first_block = cbo_get_first_block_name();1314if ($first_block) {15$classes[] = 'has-block-' . sanitize_title($first_block);16}1718return $classes;19}20add_filter('body_class', 'add_first_block_class');
cbo_block_preview($block)
Generates a preview image for a block in the Gutenberg editor.
Parameters:
$block: The Gutenberg block.
1// Use in an ACF block render callback2function my_acf_block_render_callback($block) {3// Display preview image in the editor4cbo_block_preview($block);56// Block content7$title = get_field('title');8$content = get_field('content');910echo '<div class="my-block">';11echo '<h2>' . $title . '</h2>';12echo '<div class="content">' . $content . '</div>';13echo '</div>';1415// Close the hidden div if in preview mode16if (isset($block['data']['preview_image'])) {17echo '</div>';18}19}2021// Register ACF block22function register_acf_blocks() {23acf_register_block_type([24'name' => 'my-block',25'title' => 'My Block',26'description' => 'A custom block with preview.',27'render_callback' => 'my_acf_block_render_callback',28'category' => 'formatting',29'icon' => 'admin-comments',30'keywords' => ['custom', 'block'],31'supports' => [32'align' => true,33'mode' => true,34'jsx' => true35],36]);37}38add_action('acf/init', 'register_acf_blocks');
cbo_block_anchor($block)
Generates an HTML anchor for a given block.
Parameters:
$block: The Gutenberg block.
1// Use in an ACF block render callback2function hero_block_render_callback($block) {3// Generate anchor if set in the block4cbo_block_anchor($block);56// Block content7$title = get_field('title');8$subtitle = get_field('subtitle');9$image = get_field('background_image');1011echo '<div class="hero-block">';12if ($image) {13echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" class="hero-background">';14}15echo '<div class="hero-content">';16echo '<h1>' . $title . '</h1>';17echo '<p>' . $subtitle . '</p>';18echo '</div>';19echo '</div>';20}2122// This will output something like:23// <div class="cbo-anchor" id="my-custom-anchor"></div>24// if an anchor is set in the block settings
cbo_template_block($slug, $args)
Generates an ACF block from its slug.
Parameters:
$slug: The slug of the ACF block (without the 'block-' prefix).$args: Arguments to pass to the block (default: []).
1// Render a hero block2cbo_template_block('hero', [3'title' => 'Welcome to Our Website',4'subtitle' => 'Learn more about our services',5'button_text' => 'Get Started',6'button_url' => '/services'7]);89// Render a features block10cbo_template_block('features', [11'title' => 'Our Features',12'features' => [13[14'title' => 'Feature 1',15'description' => 'Description of feature 1'16],17[18'title' => 'Feature 2',19'description' => 'Description of feature 2'20]21]22]);
cbo_template_part($slug, $args, $tpl)
Generates a template part from its slug.
Parameters:
$slug: The slug of the template part.$args: Arguments to pass to the template part (default: []).$tpl: The name of the template file without the .php extension (default: 'template').
1// Include a header template part2cbo_template_part('header', [3'title' => 'Page Title',4'subtitle' => 'Page Subtitle'5]);67// Include a custom card template part8cbo_template_part('card', [9'title' => 'Card Title',10'content' => 'Card content goes here',11'image' => get_field('card_image'),12'link' => get_field('card_link')13]);1415// Include a specific template variation16cbo_template_part('button', [17'text' => 'Click Me',18'url' => '/contact',19'style' => 'primary'20], 'rounded'); // Will load templates/parts/button/rounded.php
Usage Examples
Creating a Page Builder
1// Create a simple page builder function2function render_page_sections($post_id = null) {3if (!$post_id) {4$post_id = get_the_ID();5}67// Get sections from ACF flexible content field8$sections = get_field('page_sections', $post_id);910if (!$sections) {11return;12}1314// Render each section15foreach ($sections as $section) {16$section_type = $section['acf_fc_layout'];1718switch ($section_type) {19case 'hero':20cbo_template_block('hero', [21'title' => $section['title'],22'subtitle' => $section['subtitle'],23'background_image' => $section['background_image'],24'button_text' => $section['button_text'],25'button_url' => $section['button_url']26]);27break;2829case 'features':30cbo_template_block('features', [31'title' => $section['title'],32'features' => $section['features']33]);34break;3536case 'testimonials':37cbo_template_block('testimonials', [38'title' => $section['title'],39'testimonials' => $section['testimonials']40]);41break;4243case 'cta':44cbo_template_part('cta', [45'title' => $section['title'],46'content' => $section['content'],47'button_text' => $section['button_text'],48'button_url' => $section['button_url']49]);50break;51}52}53}5455// Usage in a template file56get_header();57render_page_sections();58get_footer();
Best Practices
- Use cbo_block_preview to provide visual previews of blocks in the editor.
- Use cbo_block_anchor to create linkable sections on the page.
- Use cbo_template_block for reusable ACF blocks.
- Use cbo_template_part for smaller, reusable template components.
- Organize template parts in a logical folder structure for better maintainability.