How to add ACF PRO to your WordPress project using Composer

Advanced Custom Fields PRO (ACF PRO) is a powerful tool for extending WordPress capabilities. In this article, we’ll show you how to integrate ACF PRO into your WordPress project using Composer, configure your license using a JSON file, and hide the ACF admin interface so fields can only be created via code.

Configure Composer

First, make sure you have Composer installed on your system. You can download it from getcomposer.org.

Generate API credentials

1. Go to your ACF account, under the licenses tab, and generate an auth.json file with your API credentials.

2. The file should have the following format:

{
    "http-basic": {
        "connect.advancedcustomfields.com": {
            "username": "YOUR_LICENSE_KEY_HERE",
            "password": "https://yoursite.com"
        }
    }
}

 

Configure Composer

Add the ACF repository to your composer.json file:

{
    "repositories": [
        {
            "type": "composer",
            "url": "https://connect.advancedcustomfields.com"
        }
    ],
    "require": {
        "wpengine/advanced-custom-fields-pro": "^6.0"
    }
}

 

Include the plugin in your project

Composer will not automatically include the plugin in your project, so you need to manually include it in your functions.phpor your custom plugin:

<?php
require_once __DIR__ . '/vendor/wpengine/advanced-custom-fields-pro/acf.php';

 

Hide the ACF admin

To ensure fields can only be created via code and not through the graphical interface, add the following code to your functions.phpor custom plugin:

<?php
add_filter('acf/settings/show_admin', '__return_false');

 

Create fields via code

You can create your fields using the acf_add_local_field_group()function. Here’s an example of how to do it:

<?php
if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array (
    'key' => 'group_1',
    'title' => 'Custom Fields Group',
    'fields' => array (
        array (
            'key' => 'field_1',
            'label' => 'Custom Field',
            'name' => 'custom_field',
            'type' => 'text',
        ),
    ),
    'location' => array (
        array (
            array (
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post',
            ),
        ),
    ),
));

endif;

By following these steps, you will have integrated ACF PRO into your WordPress project using Composer, configured your license via a JSON file, and hidden the ACF admin interface so that fields can only be created via code. This will allow you to maintain stricter control over custom fields and improve the management of your project.

We hope this guide has been helpful! If you have any questions or comments, please leave them below.

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