W
WordPress SOPKnowledge Base
Search
← All topics

ACF — Advanced Custom Fields

runnable

ACF field group registration via PHP code, getting field values in templates, repeater field loops, options pages for site-wide settings, ACF JSON sync for version control, and Elementor Pro dynamic tag integration.

acfcustom-fieldsphprepeateroptions-page
Agent trigger phrases: acf wordpress · advanced custom fields · acf field group php · get_field acf · acf repeater · acf options page · acf elementor dynamic · acf json sync

ACF extends WordPress with custom fields attached to posts, pages, CPTs, users, taxonomy terms, or options pages.

Register Field Group via PHP (Preferred — No DB Dependency)

add_action('acf/init', function() {
    acf_add_local_field_group([
        'key'    => 'group_service_fields',
        'title'  => 'Service Fields',
        'fields' => [
            [
                'key'           => 'field_service_icon',
                'label'         => 'Icon',
                'name'          => 'service_icon',
                'type'          => 'image',
                'return_format' => 'url',
            ],
            [
                'key'     => 'field_service_price',
                'label'   => 'Price',
                'name'    => 'service_price',
                'type'    => 'number',
                'prepend' => '$',
            ],
            [
                'key'        => 'field_service_features',
                'label'      => 'Features',
                'name'       => 'service_features',
                'type'       => 'repeater',
                'sub_fields' => [
                    [
                        'key'   => 'field_feature_text',
                        'label' => 'Feature',
                        'name'  => 'feature_text',
                        'type'  => 'text',
                    ],
                ],
            ],
        ],
        'location' => [
            [[
                'param'    => 'post_type',
                'operator' => '==',
                'value'    => 'service',
            ]],
        ],
    ]);
});

Getting Field Values

// Current post in template
$icon  = get_field('service_icon');
$price = get_field('service_price');

// Specific post ID
$icon = get_field('service_icon', 123);

// Repeater field
$features = get_field('service_features');
if ($features) {
    foreach ($features as $row) {
        echo esc_html($row['feature_text']);
    }
}

// Options page field
$phone = get_field('company_phone', 'option');

Options Page

// Register in functions.php
if (function_exists('acf_add_options_page')) {
    acf_add_options_page([
        'page_title' => 'Site Settings',
        'menu_title' => 'Site Settings',
        'menu_slug'  => 'site-settings',
        'capability' => 'edit_posts',
        'redirect'   => false,
    ]);
}

ACF + Elementor Pro Dynamic Tags

  1. Elementor widget > click Dynamic Data icon
  2. Select "ACF Field"
  3. Choose field name
  4. Works with: text, image, URL, color, number field types

ACF JSON Sync (Version Control)

// Save field groups as JSON files in theme
add_filter('acf/settings/save_json', function() {
    return get_stylesheet_directory() . '/acf-json';
});

// Load field groups from JSON
add_filter('acf/settings/load_json', function($paths) {
    $paths[] = get_stylesheet_directory() . '/acf-json';
    return $paths;
});

Commit /acf-json/ directory to git. Field groups sync automatically across environments.