Inline Hooks

Inline Hooks let developers use fields hooks directly within its registration with acf_add_local_field_group(). This method allow to regroup all fields behaviors at the same place and make the code portable.

Usage Example

Here is a general usage example. You’ll find more detailed usages and a list of all available hooks below.

acf_add_local_field_group(array(
    'key'    => 'group_1',
    'title'  => 'My Group',
    'fields' => array(
        array(
            'label'     => 'My Text',
            'key'       => 'field_my_text',
            'name'      => 'my_text',
            'type'      => 'text',
            'callback'  => array(
            
                /* 
                 * acf/prepare_field
                 */
                'prepare_field' => function($field){
                    
                    $field['label'] = 'Company';
                    
                    return $field;
                    
                },
            
                /* 
                 * acf/validate_value
                 */
                'validate_value' => function($valid, $value, $field, $input){
                        
                    if(strpos($value, 'Old Company Name') !== false)
                        return 'Please remove mention of "Old Company Name".';
                        
                    return $valid;
                    
                }
            
            )
        )
    )
));

Load Field

This hook is a proxy of acf/load_field. See documentation.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'load_field' => function($field){
            
            $field['label'] = 'New Label';
            
            return $field;
            
        }
    
    )
);

Prepare Field

This hook is a proxy of acf/prepare_field. See documentation.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'prepare_field' => function($field){
            
            $field['label'] = 'New Label';
            
            return $field;
            
        }
    
    )
);

Pre Render Field

This hook will be executed before the acf/render_field action.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'pre_render_field' => function($field){
            
            echo 'This text will render before the field';
            
        }
    
    )
);

Replace Render Field

This hook let you replace the builtin acf/render_field action. It is useful to replace a specific field render.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'replace_render_field' => function($field){
            
            echo 'This text will replace the field render';
            
        }
    
    )
);

Render Field

This hook will be executed after the acf/render_field action. See documentation.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'render_field' => function($field){
            
            echo 'This text will render after the field';
            
        }
    
    )
);

Load Value

This hook is a proxy of acf/load_value. See documentation.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'load_value' => function($value, $post_id, $field){
            
            $value = str_replace('text', 'content', $value);
            
            return $value;
            
        }
    
    )
);

Update Value

This hook is a proxy of acf/update_value. See documentation.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'update_value' => function($value, $post_id, $field){
            
            $value = str_replace('text', 'content', $value);
            
            return $value;
            
        }
    
    )
);

Format Value

This hook is a proxy of acf/format_value. See documentation.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'format_value' => function($value, $post_id, $field){
            
            $value = str_replace('text', 'content', $value);
            
            return $value;
            
        }
    
    )
);

Validate Value

This hook is a proxy of acf/validate_value. See documentation.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'validate_value' => function($valid, $value, $field, $input){
                
            if(strpos($value, 'Old Company Name') !== false)
                return 'Please remove mention of "Old Company Name".';
                
            return $valid;
            
        }
    
    )
);

Delete Value

This hook is a proxy of acf/delete_value.

$field = array(
    'label'     => 'My Text',
    'key'       => 'field_my_text',
    'name'      => 'my_text',
    'type'      => 'text',
    'callback'  => array(
    
        'delete_value' => function($post_id, $field_name, $field){
            
            // do_something();
            
        }
    
    )
);