Include Advanced Custom Fields in Sage 9

I have successfully managed to include Advanced Custom Fields in Sage 8 by using the following tutorial -> https://imagewize.com/advanced-custom-fields-sage/

But this does not work anymore in Sage 9, have anyone else been able to include Advanced Custom Fields into Sage 9?

2 Likes

I am running into this issue as well. I am using get_field() just as I have in Sage 8, but it does not work.

@tobias Did you manage to include the ACF in Sage 9?

As per the instructions in functions.php, you would need to put your ACF folder in /resources/, not the theme root. Sage9 modifies calls to get_stylesheet_directory() to point at that subdirectory, which may be causing ACF to not load.

2 Likes

I managed to include ACF PRO in Sage 9. Here’s how:

Note: If you need only the ACF free version just add the dependency from wpackagist like it’s described here

I used the ACF pro installer by Philipp Baschke so I added the repository to my composer.json like so:

  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "advanced-custom-fields/advanced-custom-fields-pro",
        "version": "5.6.4",
        "type": "library",
        "dist": {
          "type": "zip",
          "url": "https://connect.advancedcustomfields.com/index.php?p=pro&a=download"
        },
        "require": {
          "philippbaschke/acf-pro-installer": "^1.0",
          "composer/installers": "^1.0"
        }
      }
    }
  ],

and then required ACF:

"advanced-custom-fields/advanced-custom-fields-pro": "*"

then I added the following filters to my filters.php file and included the path to the ACF main file:

/**
 * Customize ACF path
 */
add_filter('acf/settings/path', function ( $path ) {

    $path = get_stylesheet_directory() . '/../vendor/advanced-custom-fields/advanced-custom-fields-pro/';

    return $path;

});

/**
 * Customize ACF dir
 */
add_filter('acf/settings/dir', function ( $dir ) {

    $dir = get_stylesheet_directory_uri() . '/../vendor/advanced-custom-fields/advanced-custom-fields-pro/';

    return $dir;

});

/**
 * Hide ACF field group menu item
 */
// add_filter('acf/settings/show_admin', '__return_false');

/**
 * include ACF
 */
include_once( get_stylesheet_directory() . '/../vendor/advanced-custom-fields/advanced-custom-fields-pro/acf.php' );

to submit your ACF Pro key just create an .env file in your theme directory and put it there:

ACF_PRO_KEY=PASTE_YOUR_ACF_PRO_KEY_HERE

now run composer update and you are good to go.

I also recommend creating a folder named acf-json in your resources folder. The plugin will then automatically detect it and save your field groups in json format. Find it in the ACF documentation

4 Likes

First of all, thanks to all the Roots team for the amazing Sage 9 theme (and for the Roots suite!), I’m new to it but I already see how it will help me develop and maintain WordPress themes.

As many here I use Philipp Baschke’s installer to install ACF Pro with Bedrock and Sage 9.

My problem is that when I run composer install the advanced-custom-fields folder gets installed in the vendor directory but there is no ACF plugin listed in the Plugins page of the WordPress admin.

Is there a step I’m missing? I run the exact same code as @janman22 except for the filters.php file that is not referenced in the official plugin documentation. Also in my case the ACF pro version is 5.6.9

The problem is probably trivial, but I couldn’t find an answer online, altought I’m not the first one with this issue.

Thanks in advance for your help :slightly_smiling_face:

1 Like

Well, I finally managed to solve my problem. I don’t really understand what went wrong with my first tries, so in case it could be useful for someone, here is a gist with the modified bedrock composer.json to install ACF Pro 5.6.9 in a Bedrock/Sage configuration.

1 Like

The easiest way to include ACF is imply to unzip the plugin in to your /resources/ folder and rename it to just “acf”. Then put the following in to your setup.php file at the bottom:

// 1. customize ACF path
add_filter(‘acf/settings/path’, NAMESPACE. ‘\my_acf_settings_path’);
function my_acf_settings_path( $path ) {
// update path
$path = get_stylesheet_directory() . ‘/acf/’;
// return
return $path;
}
// 2. customize ACF dir
add_filter(‘acf/settings/dir’, NAMESPACE . ‘\my_acf_settings_dir’);
function my_acf_settings_dir( $dir ) {
// update path
$dir = get_stylesheet_directory_uri() . ‘/acf/’;
// return
return $dir;
}
// 3. Hide ACF field group menu item
//add_filter(‘acf/settings/show_admin’, ‘__return_false’);
// 4. Include ACF
include_once( get_stylesheet_directory() . ‘/acf/acf.php’ );
});

This will work fine with no more adjustments necessary.

1 Like