• Hello.

    How does default values work in UM? As they doesn’t seem to work as in any other non-UM forms, or they’re not working at all.

    I assumed that setting a default value to a field, that field, if left empty, will still be displayed in view mode with the set default value. But it’s not displayed at all, as if no default value was set.

    I’ve seen another topic with 2 people having the same issue, it’s from almost 3 years ago, so I can’t reply there, but it seems that this issue still exists.

    Is there a fix to this problem? Even some code to fix this behavior would be nice, until a proper fix will be added in future updates.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Aswin Giri

    (@aswingiri)

    Hello @ovidiu-zeicu

    On the current version of the plugin, the Default value or field with a default value is displayed only in edit mode.

    Thread Starter Ovidiu Zeicu

    (@ovidiu-zeicu)

    Hello Aswin.

    If that’s the case, this default value behaves just as a placeholder. So now we have 2 placeholders. I don’t understand the logic behind this decision, but hey… each with their own ??

    Anyway… I do need that those values to be also displayed on view mode. Or, at least, I need the default value of one particular custom field to be displayed in view mode. Is there a way of doing it? A function or something? I’ve found some functions that does something similar, but my PHP is not strong enough to modify those functions to work for me.

    Thank you.

    @ovidiu-zeicu

    You can try this modified version from UM Documentation:
    “How to display fallback value if the field value is empty.”

    https://docs.ultimatemember.com/article/1548-how-to-display-fallback-value-if-the-field-value-is-empty

    function um_field_value_fallback( $value, $default, $key, $type, $data ) {
    
    	if ( empty( $value ) && isset( UM()->fields()->viewing ) && UM()->fields()->viewing === true ) {
    		$fields_without_metakey = UM()->builtin()->get_fields_without_metakey();
    		if ( !in_array( $type, $fields_without_metakey ) ) {
    			$value = $default;
    		}
    	}
    
    	return $value;
    }
    add_filter( 'um_field_value', 'um_field_value_fallback', 50, 5 );
    Thread Starter Ovidiu Zeicu

    (@ovidiu-zeicu)

    Hello @missveronicatv and thank you.

    Yes, that’s one of the codes I’ve used and tried to modify, but it displays the value only on the form, I’m not able to further work with that fallback value. For example, it won’t be visible as tagline field in members directory or in emails. So… what I basically need is that the default values to be written to the database and reusable, the fields that doesn’t have a manually added value, but have a default value set, will actually use that default value as a real value. Because, as I said before, at this point, this default/fallback value works just as another placeholder.

    Thank you.

    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hi @ovidiu-zeicu

    Sorry for the late response.

    You can try pre-populating the fields that have empty values and use the default value instead after updating the profile:

    add_action("um_user_pre_updating_profile_array","um_072522_update_profile_with_default", 10, 2);
    um_072522_update_profile_with_default( $to_update, $user_id ){
       
       foreach( $to_update as $k => $v ){
             if( empty( $v ) ){  
                 $field = UM()->fields()->get_field( $k );
                 $to_update[ $k ] = $field['default'];
             }
       }
       return $to_update;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Default Values Not Working in Forms’ is closed to new replies.