SpeakInCode
8/16/2018 - 9:49 PM

Dynamic ACF Field Population

Used on Utah Outdoor Association for the anchor links in flexible content sections

function acf_load_anchor_links( $field ) {
    // reset choices
    $field['choices'] = array();

    // get the textarea value from options page without any formatting
    $choices = get_field('anchor_links', 'option');

    // loop through array and add to field 'choices'
    if( is_array($choices) ) {
        foreach( $choices as $choice) {
            $field['choices'][sanitize_title($choice['title'])] = $choice['title'];
        }
    }

    // return the field
    return $field;
}
add_filter('acf/load_field/name=anchor_link', 'acf_load_anchor_links');