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 name
2
$first_block = cbo_get_first_block_name();
3
4
if ($first_block) {
5
echo 'The first block is: ' . $first_block;
6
} else {
7
echo 'No blocks found on this page.';
8
}
9
10
// Use the first block name to add a specific class to the body
11
function add_first_block_class($classes) {
12
$first_block = cbo_get_first_block_name();
13
14
if ($first_block) {
15
$classes[] = 'has-block-' . sanitize_title($first_block);
16
}
17
18
return $classes;
19
}
20
add_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 callback
2
function my_acf_block_render_callback($block) {
3
// Display preview image in the editor
4
cbo_block_preview($block);
5
6
// Block content
7
$title = get_field('title');
8
$content = get_field('content');
9
10
echo '<div class="my-block">';
11
echo '<h2>' . $title . '</h2>';
12
echo '<div class="content">' . $content . '</div>';
13
echo '</div>';
14
15
// Close the hidden div if in preview mode
16
if (isset($block['data']['preview_image'])) {
17
echo '</div>';
18
}
19
}
20
21
// Register ACF block
22
function register_acf_blocks() {
23
acf_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' => true
35
],
36
]);
37
}
38
add_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 callback
2
function hero_block_render_callback($block) {
3
// Generate anchor if set in the block
4
cbo_block_anchor($block);
5
6
// Block content
7
$title = get_field('title');
8
$subtitle = get_field('subtitle');
9
$image = get_field('background_image');
10
11
echo '<div class="hero-block">';
12
if ($image) {
13
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" class="hero-background">';
14
}
15
echo '<div class="hero-content">';
16
echo '<h1>' . $title . '</h1>';
17
echo '<p>' . $subtitle . '</p>';
18
echo '</div>';
19
echo '</div>';
20
}
21
22
// 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 block
2
cbo_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
]);
8
9
// Render a features block
10
cbo_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 part
2
cbo_template_part('header', [
3
'title' => 'Page Title',
4
'subtitle' => 'Page Subtitle'
5
]);
6
7
// Include a custom card template part
8
cbo_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
]);
14
15
// Include a specific template variation
16
cbo_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 function
2
function render_page_sections($post_id = null) {
3
if (!$post_id) {
4
$post_id = get_the_ID();
5
}
6
7
// Get sections from ACF flexible content field
8
$sections = get_field('page_sections', $post_id);
9
10
if (!$sections) {
11
return;
12
}
13
14
// Render each section
15
foreach ($sections as $section) {
16
$section_type = $section['acf_fc_layout'];
17
18
switch ($section_type) {
19
case 'hero':
20
cbo_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
]);
27
break;
28
29
case 'features':
30
cbo_template_block('features', [
31
'title' => $section['title'],
32
'features' => $section['features']
33
]);
34
break;
35
36
case 'testimonials':
37
cbo_template_block('testimonials', [
38
'title' => $section['title'],
39
'testimonials' => $section['testimonials']
40
]);
41
break;
42
43
case 'cta':
44
cbo_template_part('cta', [
45
'title' => $section['title'],
46
'content' => $section['content'],
47
'button_text' => $section['button_text'],
48
'button_url' => $section['button_url']
49
]);
50
break;
51
}
52
}
53
}
54
55
// Usage in a template file
56
get_header();
57
render_page_sections();
58
get_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.