Custom ACF Fields
Standard ACF fields cover most needs, but sometimes we require more visual and intuitive fields that the default plugin does not provide. Terra uses custom ACF fields to extend functionality and improve the editor experience.
Why Use Custom Fields
Section titled βWhy Use Custom FieldsβCustom fields allow us to:
- Provide visual controls that are easier for editors to use.
- Handle specific project requirements that standard fields cannot cover.
- Maintain consistency across different projects by reusing the same field setup.
Common Examples at Terra
Section titled βCommon Examples at TerraβSome frequently used custom fields include:
- Section Spacing: A field that allows editors to adjust spacing visually between sections.

- Background Color: Shows the exact color the editor is selecting, rendering the input with a live color preview.

These fields improve the user experience by making it clear and intuitive what the selected values represent.
Implementation
Section titled βImplementationβCustom ACF fields in Terra are typically organized in a dedicated folder within each project and include:
- PHP: Registers the custom field and defines its behavior.
- JS: Enhances the field in the editor with dynamic or interactive functionality.
- CSS: Styles the field for visual clarity and consistency.
This separation ensures that custom fields can be easily reused across multiple projects while keeping the code organized and maintainable.
Typical folder structure for custom ACF fields in Terra projects
Section titled βTypical folder structure for custom ACF fields in Terra projectsβfunctions/project/utilities/acf/
ββ acf-spacing/β ββ init.phpβ ββ class-PREFIX-acf-field-spacing # PHP logicβ ββ assets # CSS for visual stylingβ β ββ cssβ β ββ fields.cssβ ββ ββ imagesβ ββ js # JS for editor enhancementsβ ββ field.jsβββ acf-all-backgrounds/ ββ init.php ββ class-PREFIX-acf-field-allbackgroundcolor # PHP logic ββ assets # CSS for visual styling β ββ css β ββ background-color.css β ββ js # JS for editor enhancements ββ background-color.jsUsing Custom Fields in Code
Section titled βUsing Custom Fields in CodeβWhen using custom ACF fields in PHP, we often apply CSS classes to convert raw data into values directly usable in the frontend. A simple example:
<?php$title = $module['title'];$subtitle = $module['subtitle'];$spacing = get_spacing($module['section']['spacing']);$bg_color = $module['section']['bg_color'] ?? 'f--background-d';?>
<section class="<?= esc_attr($spacing) ?> <?= esc_attr($bg_color) ?>"> <h2 class="c--title"><?= esc_html($title); ?></h2> <p class="c--subtitle"><?= esc_html($subtitle); ?></p></section>In this example:
get_spacing()is a helper function that converts the custom spacing field value into a CSS class.- The background color field is applied as a class to the section.
- Title and subtitle fields are safely escaped and rendered with their own CSS classes for consistent styling.
This approach ensures that editors can select values visually, and developers can use them directly in the code with proper classes, keeping the frontend consistent and maintainable.
Summary
Section titled βSummaryβCustom ACF fields, combined with helper functions and CSS classes, provide a flexible and user-friendly way to manage data. Editors benefit from intuitive controls, and developers can integrate the values cleanly into the frontend, ensuring consistency, reusability, and maintainability.
β Continue to Repeater & Relationship
Knowledge Check
Test your understanding of this section
Loading questions...