Field Helper Functions For ACF Pro
by Andrew Whittaker on WordPress.org
A collection of helper functions to make working with Advanced Custom Fields Pro in your WordPress themes even easier.
This plugin provides a set of simple, powerful helper functions to reduce boilerplate code and streamline the process of retrieving and displaying ACF field values in your templates. It includes helpers for getting single values (with support for nested groups), and for easily looping over repeater fields. The plugin is built with security and best practices in mind, ensuring that all output can be properly escaped.
Features:
get_acf(): A versatile function to get any field value. It supports dot notation for nested group fields and automatically handlesget_sub_field()context within repeaters.repeater(): A clean way to loop through repeater fields with callbacks. Includes optional callbacks for before and after the main loop, and allows passing an arguments array to the row callback. (Requires ACF Pro)get_repeater_field(): Fetch a specific sub-field value from a specific row, or an array of all values for a sub-field across all rows. (Requires ACF Pro)- Lightweight and secure.
- Logs to the PHP error log for easy debugging.
Usage Examples:
get_acf()
This is your main function for retrieving any ACF field value.
-
Basic Usage: Get the value of a text field named
page_headline. -
Providing a Default Value: If the
page_subtitlefield might be empty, provide a fallback. -
Escaping HTML: When outputting a value inside an HTML attribute, always escape it.
";
Other escape options: html, url, js, text.
-
Getting a Value from an Options Page: To get a field value from an ACF Options Page, pass
optionas the$post_id. -
Getting a Value from a Nested Group Field: Use dot notation to access fields inside a group.
-
Automatic Sub Field Usage: Inside a standard ACF
have_rowsloop,get_acf()automatically usesget_sub_field()so you don’t have to change your function calls.' . get_acf('member_name', '', 'html') . ''; endwhile; endif;
repeater() (ACF Pro Only)
This function simplifies looping over repeater fields.
-
Basic Repeater Loop: To display a list of testimonials from a repeater named
testimonialswith a sub-fieldtestimonial_text:' . get_acf('testimonial_text', '', 'html') . ''; }); -
Using Before and After Callbacks: To wrap your repeater output in
andtags.' . get_acf('service_name') . '';
},
[], // No args needed for this example
function() { // Before callback
echo ' -
Passing Arguments to the Callback: To make your repeater template more reusable, pass in arguments.
'faq-item', 'heading_level' => 'h3' ]; repeater( 'faqs', function($index, $args) { printf('', esc_attr($args['item_class'])); printf( '<%s>%s%s>', esc_attr($args['heading_level']), esc_html(get_acf('question')), esc_attr($args['heading_level']) ); echo ''; }, $args );' . get_acf('answer', '', 'html') . '
- ';
},
function() { // After callback
echo '
get_repeater_field() (ACF Pro Only)
Use this to get data from a repeater field without looping through it manually.
-
Get All Values from a Sub-Field:
Total attendees: ' . count($all_attendees) . ''; -
Get a Value from a Specific Row: