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.

WordPress Fields API Project

New interesting side project of WordPress core in the making to make adding fields or custom content types more easily. This project started a few months ago and just received a huge funding by 10up.com in the form of 100 hours of paid work for one of the main developers for this project , Scott Clark . You can follow developments on Github here as well at Make WordPress here.

What is the WordPress Fields API Project?

To quote Scott at Make WordPress:

… project was nicknamed “Metamorphosis” and subsequently went by the “Metadata UI/API” project. As plugin authors dealing with and using custom fields and content types, we had been tackling the issues of developing for multiple object types in WordPress for years prior to getting together.
 

So this project is to make it easier for WordPress developers to work with custom fields , custom content types and meta boxes . Elements in WordPress Theme and WordPress developers have been working with and struggling with for years.

Use of a WordPress Fields API Project

To be fair, custom post types are fairly easy to create, certainly with sites like GenerateWP and custom fields fairly easily with Advanced Custom Fields or CMB . But what if some of the things you can do with these plugins or sites could be integrated to make it easier to code these custom content types in WordPress with less code and without the need for these plugins? That would be awesome wouldn’t it be?

WordPress Fields API  and Customizer

WordPress Fields API Project API will , unlike all the plugins out there that help creating meta boxes content types and fields, be fully integrated in the Customizer and will make sure all the code for it follows WordPress coding rules and general good practise of coding in PHP and so on.

I must say I am really excited about all this and will keep track of this great project. I was really happy with the integration of responsive images in WordPress 4.4, but better integration of metaboxes, custom content types and fields will make me just as happy or even happier!