Documentation

acf_load_field

Overview

This action works on version 3.5.8.2 and below. If using version 4, please use acf/load_field

Description

This hook allows you to modify a field after it is retrieved from the database / registered code.

Requirements

  • ACF version 3.5.1 or above

Parameters

  • $field – the field array. This can be modified and then returned

Usage

There are 3 ways to hook into acf_load_field.

  1. acf_load_field – filter for every field
  2. acf_load_field-{$field_name} – filter for a specific field based on it’s name
  3. acf_load_field-{$field_key} – filter for a specific field based on it’s key

This code is used in the functions.php file

<?php

function my_acf_load_field( $field )
{
    $field['choices'] = array(
        'custom' => 'My Custom Choice'
    );

    return $field;
}

// acf_load_field - filter for every field
add_filter('acf_load_field', 'my_acf_load_field');

// acf_load_field-{$field_name} - filter for a specific field based on it's name
add_filter('acf_load_field-my_select', 'my_acf_load_field');

// acf_load_field-{$field_key} - filter for a specific field based on it's key
add_filter('acf_load_field-field_508a263b40457', 'my_acf_load_field');
?>

Sub Field Usage

This filter can also be used for sub fields. This however requires the latest version on github (3.5.8.1) to work. It works much the same as above, only the {$field_name} variable is different. Below are examples for the repeater and flexible content fields:

Repeater

Hook is made up of parent_key-child_key, where the ‘key’ can be field_name, field_key or field_type

<?php

function my_acf_load_field( $sub_field )
{
    $sub_field['choices'] = array(
        'custom' => 'My Custom Choice'
    );

    return $sub_field;
}

// acf_load_field-{$field_name}-{$sub_field_name} - filter for a specific field based on it's name
add_filter('acf_load_field-my_repeater-my_select', 'my_acf_load_field');

// acf_load_field-{$field_key}-{$sub_field_key} - filter for a specific field based on it's key
add_filter('acf_load_field-field_1-field_2', 'my_acf_load_field');

?>

Flexible Content

Hook is made up of parent_key-layout_name-child_key, where the ‘key’ can be field_name, field_key or field_type

<?php

function my_acf_load_field( $sub_field )
{
    $sub_field['choices'] = array(
        'custom' => 'My Custom Choice'
    );

    return $sub_field;
}

// acf_load_field-{$field_name}-{$layout_name}-{$sub_field_name}: filter for a specific field based on it's name
add_filter('acf_load_field-my_flexible_content-content_with_position-my_select', 'my_acf_load_field');

// acf_load_field-{$field_key}-{$layout_name}-{$sub_field_key}: filter for a specific field based on it's key
add_filter('acf_load_field-field_1-content_with_position-field_2', 'my_acf_load_field');

?>