Support

Account

Home Forums Backend Issues (wp-admin) Auto-restore default field values?

Helping

Auto-restore default field values?

  • I have a custom field I’m using for some marketing text. I would like to allow the site owner to be able to customize that text on a per-post basis. I have set it so the field is required, and it has the boilerplate text as the default value. However, if the field content is deleted, it doesn’t repopulate the field with the default value (theoretically triggered by the conditions of an onblur event and if the field is empty).

    Is there a hidden setting to do this, or a simple way to graft this functionality onto a specific field? Thanks in advance – ACF newbie here.

  • There is no setting to restore the default value. I think you have 2 options.

    If you want the editor to see that it’s being restored to the default value, that would take writing some javascript to include with your field group. Probably the more difficult of the 2 options.

    The other option is to add a filter to check the value before the database is updated, probably the easier option.

    
    add_filter('acf/update_value/name=my_field_name', 'my_acf_update_value', 10, 3);
    function my_acf_update_value($value, $post_id, $field) {
      if (empty($value)) {
        $value = $field['default_value'];
      }
      return $value;
    }
    

    for more information see the update_value doc page: http://www.advancedcustomfields.com/resources/filters/acfupdate_value/

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

The topic ‘Auto-restore default field values?’ is closed to new replies.