Skip to content

Instantly share code, notes, and snippets.

@zackpyle
Created November 28, 2023 20:05
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 zackpyle/54ec0670849eee1790af9975d369e5d8 to your computer and use it in GitHub Desktop.
Save zackpyle/54ec0670849eee1790af9975d369e5d8 to your computer and use it in GitHub Desktop.
Adds a default image option to ACF image fields using the built in default value #ACF functionality
<?php //ignore - for gist formatting
// add default image setting to acf image field
add_action('acf/render_field_settings/type=image', 'add_default_value_to_image_field');
function add_default_value_to_image_field($field) {
acf_render_field_setting( $field, array(
'label' => 'Default Image',
'instructions' => 'Appears when creating a new post',
'type' => 'image',
'name' => 'default_value',
));
}
// show the default image
add_filter('acf/load_value/type=image', 'reset_default_image', 10, 3);
function reset_default_image($value, $post_id, $field) {
if (!$value) {
$value = $field['default_value'];
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment