Advanced Custom Fields in Sage

Sometimes you need to work with custom fields for a theme. This as you work with a template that consists of many different content fields and that tend to need different layouts too. And sometimes you do not feel like doing it all from scratch because you are lazy, or because you simply have more to do. Then Advanced Custom Fields or ACF is your friend. Simply the best GUI plugin to deal with these pesky meta boxes. So how do you put Advanced Custom Fields in Sage?

Adding ACF to Sage

Just add the entire folder inside the theme root and call it acf. If you pick another directory name make sure to change stuff accordingly here below. ACF as a name is nice and sweet and is what the guys at Advanced Custom Fields use as well.

advanced custom fields directory

 

Loading Advanced Custom Fields in Sage

Well just like in any other theme you would use the snippet suggested by ACF here. Only you would need to use namespaces like you would for working with actions and filters in general as Sage works with those. So after adjusting the snippet you would have:

 

// 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 snippet I would add to extras.php which is automatically included in the functions.php. Best place for custom code besides custom libraries or the customizer for which new files / directories would be better.

Well, once all that is done and you reload the admin you will see you are up and running. Go ahead and create your custom fields and start adding them to your pages or templates. Have fun!

NB You can uncomment hiding AC in admin for live sites where you do not want clients to play with it.

Tagged in : Tagged in : ,
Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.