ACF in code: why it’s the best option

Advanced Custom Fields (ACF) is one of the most popular plugins for adding custom fields in WordPress. Its graphical interface is very intuitive and easy to use, which makes it ideal for users without technical experience. However, when working on professional projects—especially those managed through version control systems like Git—using ACF via PHP code becomes the best practice.

In this article, we’ll explain why using ACF in code is better and show you how to do it, including working examples. We’ll also explain how to integrate it via Composer, which further improves your developer workflow.

 

Advantages of using ACF via code

1. Full git control

When you create custom fields through the WordPress admin panel, the configuration is stored in the database. That means:

  • You can’t version it with Git.
  • It’s not easily transferred between environments (local, staging, production).

 

On the other hand, if fields are registered via code, everything stays within the project in .php

files, and you can commit, track, and review changes through version control.

 

2. Reusability and modularity

You can define field groups for specific blocks, post types, or even custom Gutenberg blocks and reuse them across multiple projects.

 

3. Automated deployments

With everything in code, your deployment process becomes cleaner and more automated. There’s no need to manually export or import field configurations.

 

4. Advanced contextual logic

You can apply advanced conditional logic, like showing a field group only if a user has a specific role or a post belongs to a certain taxonomy. This is much harder (or even impossible) to achieve using only the admin UI.

 

Example: registering fields via code

First, make sure ACF PRO is active. If you’re using Composer, we’ll cover that further below.

Here’s a simple example of how to register custom fields for a Custom Post Type called project.

<?php
add_action( 'acf/init', function() {
    if ( function_exists( 'acf_add_local_field_group' ) ) {
        acf_add_local_field_group([
            'key'                   => 'group_project_info',
            'title'                 => 'Project Information',
            'fields'                => [
                [
                    'key'           => 'field_project_client',
                    'label'         => 'Client',
                    'name'          => 'client',
                    'type'          => 'text',
                ],
                [
                    'key'           => 'field_project_due_date',
                    'label'         => 'Due Date',
                    'name'          => 'dueDate',
                    'type'          => 'date_picker',
                ],
            ],
            'location'              => [
                [
                    [
                        'param'     => 'post_type',
                        'operator'  => '==',
                        'value'     => 'project',
                    ],
                ],
            ],
        ]);
    }
});

 

This code creates a field group called “Project Information” and assigns it to the projectpost type. The fields will automatically appear when editing that post type.

 

How to retrieve these fields in PHP

To retrieve ACF field values in your theme or plugin, use get_field():

<?php
$client  = get_field( 'client', get_the_ID() );
$dueDate = get_field( 'dueDate', get_the_ID() );

 

How to add ACF PRO via composer

For professional integration, the best approach is to manage ACF via composer.json. This allows you to:

  • Install it automatically with your dependencies.
  • Avoid uploading the plugin manually to every site.
  • Keep it versioned within your project.

 

Here’s a step-by-step guide to help you set it up:

How to add ACF PRO to your WordPress project using Composer

 

Although ACF’s visual interface is helpful, using code is the smarter approach as your project grows or if you’re working in a team. It gives you more control, ensures everything is versioned, synchronized across environments, and easily replicable.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to Top
0
Would love your thoughts, please comment.x
()
x