Skip to content

Instantly share code, notes, and snippets.

@sabrina-zeidan
Last active September 13, 2021 22:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sabrina-zeidan/165f357c4945b383675f6b8843bbd3a7 to your computer and use it in GitHub Desktop.
Save sabrina-zeidan/165f357c4945b383675f6b8843bbd3a7 to your computer and use it in GitHub Desktop.
Find all ACF image fields used on the site (including subfields) [WordPress]
//For example we need to modify all <img> tags in the templates that use ACF img fields.
//To locate them in the theme we need to know their names.
//This snippet will list the names of all ACF fields of image type used on the site, so that we'll be able to search for them in the theme files
$field_groups = acf_get_field_groups();
$all_fields = array();
foreach ($field_groups as $field_group){
$fields = acf_get_fields($field_group['key']);
$all_fields = array_merge($all_fields, $fields);
}
$items = $all_fields;
$all_img_subfields = [];
$all_img_fields = [];
foreach ($all_fields as $single_field){
if (!empty($single_field['sub_fields'])){
foreach($single_field['sub_fields'] as $repeater_sub_field){
if ($repeater_sub_field['type'] === 'image'){
$all_img_subfields[] = $repeater_sub_field['name'];
}
}
}
if ($single_field['type'] === 'image'){
$all_img_fields[] = $single_field['name'];
}
}
echo "<br>ACF fields names of type image saved as fields:</br>";
echo '<pre>'; print_r(array_unique($all_img_fields)); echo '</pre>';
echo "<br>ACF fields names of type image saved as sub_fields:</br>";
echo '<pre>'; print_r(array_unique($all_img_subfields)); echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment