Support

Account

Home Forums Backend Issues (wp-admin) default image field attachment

Helping

default image field attachment

  • Hello,
    I create a image acf field and I want to put a default image when I create a new post.
    Is there someone who have a solution

  • Hi,

    you can use the acf/prepare_field filter for that.
    https://www.advancedcustomfields.com/resources/acf-prepare_field/

    In the function, you first check if the value is null. Only a newly create post will have null, if it’s a saved post that has no value, it’s an empty string.

    
    <?php
    
    add_filter('acf/prepare_field/type=image', 'acf_use_default_image');
    
    function acf_use_default_image($field) {
        if ($field['value'] === null) {
            $field['value'] = 5; // attachment id
        }
    
        return $field;
    }
    

    Cheers 🤗

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

You must be logged in to reply to this topic.