A WordPress plugin that provides a framework for easily creating custom Gutenberg blocks using Advanced Custom Fields (ACF).
GT ACF Blocks simplifies the process of creating custom Gutenberg blocks for WordPress by leveraging the power of Advanced Custom Fields. This plugin allows developers to quickly build rich, customizable blocks with clean, organized code and without having to write complex JavaScript.
- Simple framework for registering custom Gutenberg blocks
- Seamless integration with Advanced Custom Fields
- Block templates with automatic field rendering
- Block categories for organization
- Block preview support
- CSS and JS enqueuing for individual blocks
- Developer-friendly API
- WordPress 5.8+
- PHP 7.4+
- Advanced Custom Fields PRO 5.8+
- Upload the
gt-acf-blocks
folder to the/wp-content/plugins/
directory - Activate the plugin through the 'Plugins' menu in WordPress
- Ensure Advanced Custom Fields PRO is installed and activated
- Create a new folder for your block in the
blocks
directory - Add your block's PHP, CSS, and JS files
- Register your block using the plugin's API
blocks/
└── example-block/
├── block.json # Block configuration
├── block.php # Block template
├── block.css # Block styles (optional)
├── block.js # Block scripts (optional)
└── fields.php # ACF field definitions
Add your block registration to your theme's functions.php
or a custom plugin:
add_action('acf/init', 'register_my_blocks');
function register_my_blocks() {
if (function_exists('gt_acf_blocks_register_block')) {
gt_acf_blocks_register_block([
'name' => 'example-block',
'title' => 'Example Block',
'description' => 'An example custom block',
'category' => 'formatting',
'keywords' => ['example', 'demo'],
]);
}
}
Follow these steps to create and register a new custom block:
-
Create the block directory structure:
cd /path/to/your/wordpress/wp-content/plugins/gt-acf-blocks/blocks mkdir my-custom-block
-
Create the necessary files:
block.json
- Block configurationblock.php
- Block templatefields.php
- ACF field definitionsblock.css
(optional) - Block stylesblock.js
(optional) - Block scripts
-
Configure your block.json:
{ "name": "my-custom-block", "title": "My Custom Block", "description": "A description of what my block does", "category": "formatting", "icon": "star-filled", "keywords": ["custom", "example"], "acf": { "mode": "preview", "renderTemplate": "block.php" }, "supports": { "align": true, "mode": false, "jsx": false } }
-
Define your ACF fields in fields.php:
<?php if (function_exists('acf_add_local_field_group')) { acf_add_local_field_group([ 'key' => 'group_my_custom_block', 'title' => 'My Custom Block', 'fields' => [ [ 'key' => 'field_my_custom_field', 'label' => 'Custom Field', 'name' => 'custom_field', 'type' => 'text', ], ], 'location' => [ [ [ 'param' => 'block', 'operator' => '==', 'value' => 'acf/my-custom-block', ], ], ], ]); }
-
Create your block template in block.php:
<?php /** * Block Template */ // Get field values $custom_field = get_field('custom_field'); ?> <div id="<?php echo esc_attr($block['id']); ?>" class="my-custom-block"> <?php if ($custom_field): ?> <div class="custom-content"> <?php echo esc_html($custom_field); ?> </div> <?php endif; ?> </div>
-
Register your block in your theme's functions.php or a custom plugin:
add_action('acf/init', 'register_my_blocks'); function register_my_blocks() { if (function_exists('gt_acf_blocks_register_block')) { gt_acf_blocks_register_block([ 'name' => 'my-custom-block', ]); } }
-
Add CSS (optional) in block.css:
.my-custom-block { padding: 20px; background-color: #f8f8f8; } .my-custom-block .custom-content { font-size: 18px; color: #333; }
-
Add JavaScript (optional) in block.js:
(function($) { $(document).ready(function() { $('.my-custom-block').on('click', function() { // Your custom JavaScript here }); }); })(jQuery);
-
Test your block by adding it to a page or post in the WordPress editor.
Each block can be configured through its block.json
file with the following options:
name
: Internal block name (required)title
: Display title in the editor (required)description
: Block descriptioncategory
: Block categoryicon
: Dashicon or SVG to represent the blockkeywords
: Search terms to help users discover the blocksupports
: Block support optionsexample
: Preview example configuration
Define your ACF fields in the block's fields.php
file:
<?php
if (function_exists('acf_add_local_field_group')) {
acf_add_local_field_group([
'key' => 'group_example_block',
'title' => 'Example Block',
'fields' => [
[
'key' => 'field_example_title',
'label' => 'Title',
'name' => 'title',
'type' => 'text',
],
// Add more fields as needed
],
'location' => [
[
[
'param' => 'block',
'operator' => '==',
'value' => 'acf/example-block',
],
],
],
]);
}
Create your block's front-end display in the block.php
file:
<?php
/**
* Block Template
*
* @param array $block The block settings and attributes
* @param string $content The block content
* @param bool $is_preview Whether this is a preview in the editor
*/
// Get field values
$title = get_field('title');
?>
<div id="<?php echo esc_attr($block['id']); ?>" class="example-block">
<?php if ($title): ?>
<h2><?php echo esc_html($title); ?></h2>
<?php endif; ?>
</div>
For support, feature requests, or bug reports, please submit an issue via the GitHub repository or send me a message.
This plugin is licensed under the GPL v2 or later.
Built with ♥ by Gaurav Tiwari