Skip to content

Instantly share code, notes, and snippets.

@billerickson billerickson/acf.php Secret
Created Nov 19, 2018

Embed
What would you like to do?
<?php
/**
* Dynamic Icon Select
* Lists icons found in theme's /assets/icons directory
*
*/
function ea_acf_icon_select( $field ) {
$field['choices'] = array( 0 => '(None)' );
if( ! function_exists( 'ea_get_theme_icons' ) )
return $field;
$icons = ea_get_theme_icons();
foreach( $icons as $icon ) {
$field['choices'][ $icon ] = $icon;
}
return $field;
}
add_filter('acf/load_field/name=icon', 'ea_acf_icon_select' );
/**
* Theme Icons
*
*/
function ea_get_theme_icons() {
$icons = get_option( 'ea_theme_icons' );
$version = get_option( 'ea_theme_icons_version' );
if( empty( $icons ) || ( defined( 'CHILD_THEME_VERSION' ) && version_compare( CHILD_THEME_VERSION, $version ) ) ) {
$icons = scandir( get_stylesheet_directory() . '/assets/icons' );
$icons = array_diff( $icons, array( '..', '.' ) );
$icons = array_values( $icons );
if( empty( $icons ) )
return $icons;
// remove the .svg
foreach( $icons as $i => $icon ) {
$icons[ $i ] = substr( $icon, 0, -4 );
}
update_option( 'ea_theme_icons', $icons );
if( defined( 'CHILD_THEME_VERSION' ) )
update_option( 'ea_theme_icons_version', CHILD_THEME_VERSION );
}
return $icons;
}
@chadsterBAM

This comment has been minimized.

Copy link

chadsterBAM commented Nov 21, 2018

This is awesome. But do you first have a field type "icon" that you're calling? I'm trying to build a gutenberg block that allows link selection with an icon and this is going to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.