Skip to content

Instantly share code, notes, and snippets.

@joelstransky
Created December 1, 2017 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelstransky/705ca0e47244d591a98dd77553525742 to your computer and use it in GitHub Desktop.
Save joelstransky/705ca0e47244d591a98dd77553525742 to your computer and use it in GitHub Desktop.
Alter Advanced Custom Field rendering in admin
<?php
/*
* This snippet is from a project where I used an ACF Options gallery field to collect a set of icons.
* This fields data was then used to replace a radio field's options with the actual icons.
*/
class Special_Excerpt_Icon {
function __construct() {
// filter load_field on all 'radio' fields
add_filter('acf/load_field/type=radio', array($this, 'on_acf_load_radio_field' ) );
}
/**
* Replaces this fields options data with the overview icons
* set in the site options' icon gallery
* @param array $field the field settings array
* @return array The modified field settings array
*/
public function on_acf_load_radio_field( $field ) {
// bail out if not requested within the admin
if (! is_admin() ) {
return $field;
}
// check against the exact field we intend to modify
if ( 'field_588c095495cb5' === $field['key'] && 'group_586bf7cc49163' === $field['parent'] ) {
// init the field's 'choices' prop
$field['choices'] = array();
$icons = get_field('overview_icons', 'options');
if($icons) {
// use ACF's native 'choices' structure
foreach ($icons as $icon) {
$field['choices'][$icon['id']] = '<img class="excerpt-icon" src="' . $icon['sizes']['thumbnail'] . '">';
}
}
}
return $field;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment