Support

Account

Home Forums Backend Issues (wp-admin) Setting Default Value in Blank fields

Helping

Setting Default Value in Blank fields

  • Hi there,

    I’m not great with php, but trying to do a few minor things with it.

    I’d like to set a default value for a User custom field that applies when the field is blank, or hasn’t been set yet. The Default Value option in ACF doesn’t seem to have the effect I’m looking for, as new users don’t get this default applied.

    How do I say “Look for the User Custom Field ‘account_terms’, if that field is blank or hasn’t been set, set it to value ‘CASH’? The below was my attempt via copy/pasting/editing from different topics in this forum.

    $value = get_field('account_terms');
    if ($value !== '') {
      // value does not exactly equal an empty string
      // it was never set
      // use a default value
      $value = 'CASH';
    }
  • If you want ACF to return the default value in the case where the field has never been updated then you need to use the field key to get the field.

    
    $value = get_field('field_XXXXXXXX');
    

    The reason for this is that when you use the field name ACF tries to get the field key reference that it stores in that DB for the field. Since the field has not been saved ACF cannot find the field definition so it does not know what to return as a default value.

    You can do it the way you’re trying but your code is a little off

    
    if ($value !== '') 
    

    will only change the value if there is a value, you want something like

    
    if (empty($value)) {
      $value = 'CASH';
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.