Support

Account

Home Forums Feature Requests Default Image option

Solving

Default Image option

  • I would like a default image option. This is good for like when I create employee pages and want to use a photo that says “coming soon” or “Image not available”.

  • I never gave this much thought, although I do the same thing the same thing every time I need to have a default image. I always need to create custom code in the template to see if there’s an image and then do the default stuff.

    Just did some testing and I can set a default image using a filter like this

     
      add_filter('acf/load_field/name=image', 'set_default_image');
      function set_default_image($field) {
        $field['default_value'] = 258;
        return $field;
      }
      
    

    Then I tried this, this code will add a default setting to every image field when you create it, it’s just a text field, I’ve just started playing with adding additional settings to ACF fields so I don’t know what else is possible.

     
      add_action('acf/render_field_settings/type=image', 'add_default_value_to_image_field', 20);
      function add_default_value_to_image_field($field) {
        acf_render_field_setting( $field, array(
          'label'      => __('Default Image ID','acf'),
          'instructions'  => __('Appears when creating a new post','acf'),
          'type'      => 'text',
          'name'      => 'default_value',
        ));
      }
      
    
  • Even better, shows an image field to select an image

    
      
      add_action('acf/render_field_settings/type=image', 'add_default_value_to_image_field', 20);
      function add_default_value_to_image_field($field) {
        acf_render_field_setting( $field, array(
          'label'      => __('Default Image ID','acf'),
          'instructions'  => __('Appears when creating a new post','acf'),
          'type'      => 'image',
          'name'      => 'default_value',
        ));
      }
    
    
  • How do you specify a default value for “acf/render_field_settings”?

    I have a “true_false” field type and it always defaults to true… I want it to default to false.

  • I think you would set 'default_value' => 0. When I have questions like this I generally create a field like the type I want to use for my field with the settings I want it to have and then do an export to code to see that the settings are.

  • When I have questions like this I generally create a field like the type I want to use for my field with the settings I want it to have and then do an export to code to see that the settings are.

    Never though of that… Thanks!!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Default Image option’ is closed to new replies.