How to Build a Custom WordPress Block with ACF and PHP

  • June 29, 2025
  • Blog
No Comments

Want to create custom WordPress blocks with ACF? This guide will walk you through making your own blocks using Advanced Custom Fields (ACF) and PHP. We’ll cover everything from getting your setup ready to building dynamic content. If you’re looking to make custom blocks without a lot of hassle, you’re in the right place.

Key Takeaways

  • ACF PRO is really good for making custom WordPress blocks, especially if you like using PHP.
  • You register your blocks using a ‘block.json’ file, which tells WordPress all about your block.
  • ACF fields let you add dynamic content to your blocks, making them flexible and easy to update.
  • You can make blocks for things like testimonials or team member profiles, pulling data from ACF fields.
  • Using PHP for block templates is often simpler for WordPress developers than diving into a lot of JavaScript.

Understanding Custom WordPress Block Development

WordPress’s block editor, often called Gutenberg, has changed how we build websites. Instead of relying on shortcodes or custom HTML, we can now create reusable blocks. This section will cover the basics of custom block development, focusing on how ACF and PHP can be used to make powerful and flexible blocks.

The Role of block.json in Block Registration

The block.json file is essential for registering a custom block in WordPress. It acts as the block’s configuration file, defining its name, title, description, attributes, and other important metadata. Think of it as the blueprint that tells WordPress how to handle your block. Without a properly configured block.json, your block won’t show up in the editor. It’s the first thing WordPress looks for when registering a block, so getting it right is key. You can manually create custom WordPress blocks by first creating a plugin to call your block files.

Leveraging PHP for Block Rendering

While block.json handles registration, PHP is often used for the actual rendering of the block’s content. This is where you fetch data from ACF fields and output the HTML that will be displayed on the front end. Using PHP gives you a lot of flexibility, allowing you to perform complex logic and data manipulation before rendering the block. It’s especially useful when you need to create dynamic blocks that pull data from various sources. PHP is a server-side language, so the rendering happens on the server before the page is sent to the browser. This can improve performance and security.

Integrating CSS for Block Styling

CSS is what makes your blocks look good. You can use CSS to style your blocks in the editor and on the front end. There are several ways to integrate CSS into your custom blocks. You can enqueue a separate CSS file for each block, or you can use inline styles. Enqueuing a separate file is generally the best practice, as it allows you to take advantage of browser caching. You can also use CSS preprocessors like Sass or Less to make your CSS more maintainable. Remember to use specific CSS classes for your blocks to avoid conflicts with other styles on your site. Proper styling is what makes your blocks visually appealing and consistent with your brand. You can create custom blocks with React and the @wordpress/create-block tool, and with the ACF Blocks feature of Advanced Custom Fields.

Setting Up Your Development Environment for ACF Blocks

Before you start building amazing custom blocks with ACF, it’s important to get your development environment squared away. This means having the right tools and structure in place so you can work efficiently and without headaches. Think of it as laying the foundation for a house – a solid base makes everything else easier.

Prerequisites: ACF PRO and WordPress Access

First things first, you’ll need ACF PRO. ACF Blocks are a feature exclusive to the PRO version, so the free version won’t cut it. Make sure you have a license and that the plugin is installed and activated on your WordPress site. Also, you’ll need access to a WordPress installation where you can experiment. It’s highly recommended to use a local development environment like Local by Flywheel or Docker. This keeps your live site safe from any potential mishaps during development. You don’t want to accidentally break your website while you’re learning!

Structuring Your Theme or Plugin for Blocks

Now, where should you put your block code? You have two main options: your theme or a plugin. While you could technically add the code directly to your theme’s functions.php file, it’s generally not a good idea. If you switch themes, your blocks will disappear! A better approach is to create a child theme or a dedicated plugin for your blocks. A child theme is great if you’re heavily customizing your existing theme. A plugin is ideal if you want your blocks to be reusable across different themes. Here’s a basic structure for a plugin:

my-acf-blocks-plugin/
├── my-custom-block/
│   ├── block.json
│   ├── render.php
│   └── style.css
├── index.php

Essential Files for Block Creation

Within your block’s directory (e.g., my-custom-block), you’ll need a few key files:

  • block.json: This file is the heart of your block. It defines the block’s properties, such as its name, title, description, and attributes. It’s what tells WordPress how to register and display your block.
  • render.php: This file contains the PHP code that renders the block’s output. It’s where you’ll retrieve data from ACF fields and generate the HTML that’s displayed on the front end.
  • style.css (Optional): This file holds the CSS styles for your block. You can use it to customize the block’s appearance in both the editor and on the front end. If you skip this, your block will inherit styles from your theme.

Setting up your development environment properly is half the battle. By having ACF PRO installed, a safe development environment, and a well-organized file structure, you’ll be well-equipped to create custom WordPress blocks with ACF like a pro. Don’t skip this step!

Registering Your First Custom WordPress Block with ACF

Code editor with WordPress block.

Time to get our hands dirty and actually register a block! It might sound intimidating, but ACF makes it surprisingly straightforward. We’ll walk through the essentials, step by step.

Defining Block Properties in block.json

The block.json file is the place where you tell WordPress everything it needs to know about your block. It’s like the block’s resume. This file includes the block’s name, title, description, category, icon, and, most importantly, how it connects to ACF. Here’s a basic example:

{
  "name": "acf/my-awesome-block",
  "title": "My Awesome Block",
  "description": "A block that displays awesome content.",
  "category": "common",
  "icon": "star",
  "keywords": ["awesome", "content"],
  "acf": {
    "mode": "preview",
    "renderTemplate": "blocks/my-awesome-block/my-awesome-block.php"
  }
}
  • name: This is the unique identifier for your block. It should start with acf/.
  • title: The human-readable name of your block, which appears in the block inserter.
  • description: A short description of what your block does.
  • category: The category your block will be listed under in the block inserter (e.g., common, formatting, widgets).
  • icon: The icon that represents your block in the block inserter. You can use a Dashicon or a custom SVG.
  • keywords: Keywords that users can search for to find your block.
  • acf: This section tells ACF how to handle the block. mode: preview means the block will render a preview in the editor. renderTemplate specifies the path to the PHP template file that will render the block’s content.

Connecting Blocks to ACF Field Groups

Now, we need to link our block to the ACF fields that will provide the dynamic content. This is done by creating an ACF field group and setting the location rules to show the field group when the block is selected. Here’s how:

  1. Go to Custom Fields > Add New in your WordPress admin.
  2. Give your field group a descriptive name (e.g., "My Awesome Block Fields").
  3. Add the fields you need for your block (e.g., text, image, WYSIWYG editor).
  4. Under Location Rules, select "Block" and then choose your block’s name (e.g., "My Awesome Block").
  5. Save the field group.

Implementing the Block Template File

The template file is where the magic happens. This PHP file is responsible for rendering the block’s content using the data from the ACF fields. The path to this file is specified in the renderTemplate property of your block.json file. Here’s a basic example:

<?php

/**
 * My Awesome Block Template.
 *
 * @param   array $block The block settings and attributes.
 * @param   string $content The block inner HTML (empty).
 * @param   bool $is_preview True during AJAX preview.
 * @param   int|string $post_id The post ID this block is saved to.
 */

// Load values and assign defaults.
$text = get_field('text') ?: 'Your text here...';

?>
<div class="my-awesome-block">
    <p><?php echo esc_html( $text ); ?></p>
</div>
  • The $block variable contains information about the block, such as its ID and class names.
  • The get_field() function retrieves the value of an ACF field. The first argument is the field name, and the second argument is optional and specifies the post ID. If you’re using the block on the current post, you can omit the second argument.
  • The esc_html() function escapes HTML entities to prevent security vulnerabilities.

Remember to place your template file in the correct directory, as specified in your block.json file. Also, make sure your theme or plugin is set up to register the block type using the register_block_type() function. This is often done in your theme’s functions.php file or in a separate plugin file.

With these steps completed, your custom block should now be available in the WordPress block editor, ready to display dynamic content powered by ACF!

Building Dynamic Content with ACF Fields

Alright, so you’ve got your block registered and ready to go. Now comes the fun part: actually making it dynamic! This means pulling in data from your ACF fields and displaying it in the block, both in the editor and on the front end. It’s what makes custom blocks truly useful.

Creating Custom Field Groups for Blocks

First things first, you need to define the fields that your block will use. This is where ACF shines. You’ll create a new field group (or use an existing one) and add all the fields you need – text fields, image fields, dropdowns, whatever your block requires. The key is to set the location rules so that the field group only appears when your specific block is selected. This keeps things organized and prevents fields from showing up where they shouldn’t. For example, if you’re building a testimonial block, you might have fields for the author’s name, their quote, and their image.

Retrieving Field Data in Your Block Template

Once your field group is set up, it’s time to access that data in your block’s PHP template file. ACF provides a simple function, get_field(), to retrieve the value of a field. You’ll pass the field name as an argument, and it will return the field’s value. It’s important to remember that you need to use the correct field name (the one you defined in ACF), and you should also sanitize the output to prevent any security issues. You can use esc_html() for text fields and wp_get_attachment_image() for image fields. This is where the magic happens – you’re connecting the data you enter in the editor to the actual output of the block. Blocks for ACF Fields simplifies this process.

Displaying Dynamic Content in the Editor

Displaying dynamic content isn’t just about the front end; it’s also about providing a good editing experience. You want the block to look as close as possible to the final output within the editor itself. This is where the block’s render_callback function comes in. This function is responsible for generating the HTML that’s displayed in the editor. By using the same get_field() calls and the same HTML structure as your front-end template, you can ensure that the block looks consistent in both places. This makes it easier for content creators to visualize the final result and make informed decisions. It’s a small detail, but it can make a big difference in the overall user experience.

It’s worth noting that ACF PRO offers even more advanced features for block development, such as the ability to create custom preview templates. These templates allow you to completely customize the way the block looks in the editor, giving you even more control over the editing experience.

Here’s a simple example:

<?php
// In your block's template file
$author_name = get_field('author_name');
$quote = get_field('quote');
$author_image = get_field('author_image');
?>

<div class="testimonial-block">
 <img src="<?php echo wp_get_attachment_image_src( $author_image, 'thumbnail' )[0]; ?>" alt="<?php echo esc_attr( $author_name ); ?>">
 <blockquote><?php echo esc_html( $quote ); ?></blockquote>
 <p>- <?php echo esc_html( $author_name ); ?></p>
</div>

This code snippet retrieves the values from the author_name, quote, and author_image fields and displays them within a simple HTML structure. Remember to adjust the HTML and CSS to match your desired design.

Advanced Techniques for Custom WordPress Blocks

Utilizing InnerBlocks for Nested Content

InnerBlocks are a game-changer when you need to create blocks that contain other blocks. Think of it like building with LEGOs – you have individual bricks (blocks) that you can combine to create more complex structures. This is especially useful for creating layouts where you want content editors to have flexibility in adding and arranging content within a specific container.

For example, you might have a "Section" block that allows editors to add multiple "Text" blocks, "Image" blocks, or even other custom blocks inside it. This provides a structured way to build complex pages while still giving content creators control over the content.

Here’s why InnerBlocks are awesome:

  • They provide a structured way to create complex layouts.
  • They allow content editors to easily add and arrange content.
  • They ensure consistency in design and structure.

Implementing Block Locking for Content Control

Block locking is a powerful feature that lets you control how content editors can interact with your custom blocks. Sometimes, you want to ensure that certain parts of a block remain unchanged to maintain the design or functionality. Block locking allows you to prevent users from moving, deleting, or editing specific blocks or attributes within a block.

There are different levels of locking you can implement:

  1. Preventing movement: Stops users from dragging and dropping the block to a new location.
  2. Disabling deletion: Prevents users from removing the block from the page.
  3. Locking attributes: Restricts users from editing specific fields within the block.

Block locking is super useful when you want to:

  • Maintain a consistent design across your website.
  • Prevent accidental changes to critical content.
  • Guide content editors to use blocks in a specific way.

Block locking is a great way to ensure that your custom blocks are used correctly and consistently. It gives you more control over the content creation process and helps to maintain the integrity of your website’s design.

Exploring Block Context for Data Sharing

Block context provides a way for blocks to share data with each other. This is particularly useful when you have blocks that need to be aware of their surrounding environment or need to access data from a parent block. Think of it as a way for blocks to "talk" to each other and exchange information.

For example, imagine you have a "Gallery" block that contains multiple "Image" blocks. You could use block context to share the gallery’s settings (like the number of columns or the image size) with the individual image blocks. This way, each image block can automatically adjust its appearance based on the gallery’s settings.

Here’s how block context can help you:

  • Create more dynamic and interactive blocks.
  • Reduce redundancy by sharing data between blocks.
  • Improve the overall user experience by making blocks more aware of their context.

Streamlining Workflow with ACF PRO for Block Creation

Why ACF PRO Excels for Custom Blocks

ACF PRO really shines when it comes to making custom blocks. It’s like having a superpower for WordPress development. Instead of getting bogged down in complex code, you can focus on design and functionality. It’s a game-changer for speeding up your workflow and creating better sites. It’s a tool that helps you avoid unnecessary headaches.

Comparing ACF Blocks to Other Block Development Methods

There are a few ways to build custom blocks in WordPress, but they all have their downsides. Some plugins are too basic, while manual coding can be overkill. ACF PRO strikes a balance, offering a PHP-based approach that’s fast, flexible, and familiar.

Consider this comparison:

Feature ACF Blocks Basic Plugins Manual Coding
Complexity Medium Low High
Customization High Limited Unlimited
Development Time Medium Fast Slow
Flexibility High Limited High

ACF PRO builds on what already makes sense. Its intuitive API and extensive documentation mean you’re not stuck parsing endless dependencies just to create a block that does what you want. Need something simple? It’s done in minutes. Complex? Extend it with familiar tools and keep full control without the extra bloat.

Accelerating Development with a PHP-Based Approach

ACF PRO’s PHP-based approach is a huge time-saver. If you already know PHP, you’re good to go. You don’t have to learn a whole new language or framework. This means you can build custom blocks faster and more efficiently. Plus, it integrates seamlessly with your existing WordPress theme. It’s all about making your development processes smarter. ACF PRO enables millions of developers to streamline their workflow, build better WordPress sites, and actually enjoy the process. You can create your first custom block today with ACF PRO.

Practical Examples of Custom ACF Blocks

Code snippets on screens with WordPress logo.

Building a Testimonial Block with ACF Fields

Let’s get practical! One of the simplest and most effective ways to see ACF Blocks in action is by building a testimonial block. This block allows you to showcase customer quotes and reviews in a visually appealing way. You’ll need ACF PRO for this, as ACF Blocks are a premium feature. The basic steps involve creating a child theme or plugin, registering the block properties in block.json, and then connecting it to an ACF field group. The field group would include fields like:

  • Text Area: For the actual testimonial quote.
  • Text: For the person’s name and role.
  • Image: For a photo of the person giving the testimonial.
  • Color Picker: To customize the background and text colors.

Once you’ve set up the fields, you can create a template file (template.php) to display the data. This file will pull the data from the ACF fields and output the HTML for the block. It’s a straightforward way to add dynamic content to your site without writing a ton of code.

Developing a Team Member Block with Dynamic Data

Want to showcase your team? A team member block is a great way to do it. This goes a step beyond the testimonial block by incorporating more dynamic data. Imagine a block that automatically updates with each team member’s information. You’ll start by creating a new field group with fields for each team member’s name, role, a short bio, and a photo. Then, you’ll set the location rules to display the field group when your custom block is selected. The template.php file will then pull data from ACF. For example:

<?php
// Retrieve ACF fields
$name = get_field('name', $block['id']);
$role = get_field('role', $block['id']);
$year = get_field('year', $block['id']);
$photo = get_field('photo', $block['id']);
?>

This block is more dynamic because it allows you to easily add, edit, and rearrange team members without having to touch the code. It’s a great example of how ACF can make content management much easier.

Creating a Slider Block Using ACF Functionality

For a more advanced example, consider building a slider block. This block lets you display a series of images or content in a rotating carousel. This is where ACF’s flexibility really shines. You can create a repeater field in ACF to manage the slides. Each slide can have its own image, title, description, and link. The template.php file will then loop through the repeater field and output the HTML for each slide. You’ll also need some JavaScript to handle the slider functionality. This could involve using a library like Slick or Swiper, or writing your own custom script. The key is to use ACF to manage the content of the slides, and then use JavaScript to handle the presentation.

Building custom blocks with ACF is a game-changer. It simplifies the process of creating dynamic and reusable content blocks for WordPress, making it easier for developers and content creators alike.

Wrapping Up

Custom blocks are super helpful for making WordPress sites more flexible. They let content editors do more cool stuff while keeping everything looking good and working right. Honestly, there’s no single "right" way to build WordPress blocks, as long as they do what you need them to. But, if you’re looking for something that’s easier to learn and quicker to get going with, building custom blocks using ACF is a really solid choice. It just makes sense, especially if you’re already comfortable with PHP and HTML. You’ll need ACF PRO, but it’s totally worth it if you’re serious about making custom blocks. ACF Blocks fit right into how WordPress works. You can build structured, reusable blocks fast, and still have full control over how they look and what they do. Instead of messing with React, you’re using a PHP system that just clicks with your theme development. A lot of agencies and freelance developers love this because it speeds things up without losing any customization. The numbers don’t lie: in 2024, over half of WordPress developers using the block editor built blocks with ACF. That just shows that ACF Blocks are often the better way to go.

Frequently Asked Questions

What exactly are ACF Blocks?

ACF Blocks are special WordPress blocks that let you use Advanced Custom Fields (ACF) to easily add and manage content. Unlike regular WordPress blocks, ACF Blocks use PHP for their main code, which is often simpler for people who already know WordPress and PHP.

What do I need to start making ACF Blocks?

You need ACF PRO to make ACF Blocks. The free version of ACF doesn’t have this feature. You’ll also need a WordPress website where you can get to the ‘wp-content’ folder. It’s a good idea to set up a practice site, like one using ‘Local’, so you can try things out safely.

Can I make an ACF Block without using any ACF fields?

Yes, you can! While ACF Blocks often work with ACF fields to pull in information, you can definitely create a block that doesn’t use any custom fields. This is useful for simpler blocks that just display static content or integrate with other parts of your site.

How do I create my first ACF Block?

Making an ACF Block involves a few simple steps: first, set up a special folder in your theme or plugin. Then, create a ‘block.json’ file to tell WordPress about your block, and a PHP file for how your block looks. You can also link it to ACF fields if you want to add custom content.

Why should I use ACF Blocks instead of other ways to make custom blocks?

ACF Blocks are great because they let you build custom blocks using PHP, which many WordPress developers already know well. This means you can create powerful and flexible blocks without needing to learn complex JavaScript frameworks. It’s a faster and more familiar way to work for many.

Can ACF Blocks handle more complex features like nested content or content controls?

Yes, you can! ACF Blocks support advanced features like ‘InnerBlocks’ for putting other blocks inside your custom block, ‘block locking’ to control what users can change, and ‘block context’ for sharing information between blocks. This lets you build really complex and smart blocks.

About this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts

Leave a Comment