Created
December 1, 2017 17:19
-
-
Save joelstransky/705ca0e47244d591a98dd77553525742 to your computer and use it in GitHub Desktop.
Alter Advanced Custom Field rendering in admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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