• Resolved ftpwp

    (@ftpwp)


    Hi,

    I have 2 checkbox custom fields on my UM user profile and…
    – I don’t want to make them visible on the Registration form,
    – but, I would like them to be checked by default as soon as the user is registered.

    Is there any setting to do that?

    Otherwise, I can do it via functions.php, using the um_after_save_registration_details hook that will give me the user ID, but I would need some help to find the line of code to assign “TRUE” to a custom field with a meta key named (for example) “profile_notification”?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hi @ftpwp,

    Please go to your checkbox field settings and add your default value to the “Default value” field and see if it helps.

    Regards.

    Thread Starter ftpwp

    (@ftpwp)

    No, it’s of course not.
    (I was sure I would get this answer, I hesitated to warn by advance that this was not a valid answer and it looks like I should really have write it.)

    Using “Default value”:
    1/ is not setting any value before you actually edit the profile and save it, so it’s absolutely not what I want.
    I want to automatically assign the value as soon as the account/profile is created.
    2/ in the context of a checkbox, it’s even completely misleading the users because when they edit their profile, they ALWAYS end up with a checkbox ticked. Even if they untick, save, edit, as unticked=empty=default value, the field always appears ticked weather it’s really ticked or not.

    So this is of course not the solution.

    Thread Starter ftpwp

    (@ftpwp)

    I think the solution is here…
    https://wordpress.org/support/topic/what-is-the-php-function-to-save-fields-in-um_user/#post-10965068
    …but it’s still not 100% clear to me and it’s not working so far.

    @wptechnology, I reply to your topic in mine as yours is closed.

    You suggest…

    global $ultimatemember;
    $user_id = um_user('ID');
    um_fetch_user($user_id);
    $toupdate[] = array('name_of_field' => 'its_value');
    UM()->user()->update_profile($toupdate);
    update_user_meta($user_id, 'name_of_field','its_value');

    In my context, 3 checkboxes to update when the profile is created, I think it would become this (in functions.php)…

    add_action('um_after_save_registration_details', 'ui_registration_enable_notifications', 10, 2);
    
    function ui_registration_enable_notifications( $user_id, $submitted ) {
    	um_fetch_user($user_id);
    
    	$toUpdate[] = array('profile_notif_new_post' => array (0 => 'enable'),
    			    'profile_notif_new_comment' => array (0 => 'enable'),
    			    'profile_notif_new_mention' => array (0 => 'enable'));
    	UM()->user()->update_profile($toUpdate);
    
    	update_user_meta($user_id, 'profile_notif_new_post', array (0 => 'enable'));
    	update_user_meta($user_id, 'profile_notif_new_comment', array (0 => 'enable'));
    	update_user_meta($user_id, 'profile_notif_new_mention', array (0 => 'enable'));
    }

    But I have several questions:

    1/ Is global $ultimatemember; still needed in my case?

    2/ What’s the differences in between the UM()->user()->update_profile() and the update_user_meta() updates? What are they respectively updating?

    3/ For $toUpdate[], can we group 3 fields to update in 1 array like I did?

    4/ I think checkbox values are set by an array like this array (0 => 'value'), correct?

    5/ For the update_user_meta() update, we need to use 3 lines of code, we can’t group the update for the 3 fields, correct?

    6/ How do we update checkboxes in meta? Also by an array like array (0 => 'value'), same?

    I ask you those questions as something must be wrong. I have no error but it’s not working, the checkboxes are not ticked in the user’s profile.

    Thanks!

    • This reply was modified 4 years, 5 months ago by ftpwp.
    Thread Starter ftpwp

    (@ftpwp)

    Or another option may be this one…
    https://docs.ultimatemember.com/article/128-add-a-hidden-field-to-your-register-form

    But then what do we put for “name”, “id” and “value” for each field?

    According to the source code of the profile page in edit mode, it could be that;
    echo '<input type="hidden" name="profile_notif_new_post[]" id="???" value="enable" />';
    echo '<input type="hidden" name="profile_notif_new_comment[]" id="???" value="enable" />';
    echo '<input type="hidden" name="profile_notif_new_mention[]" id="???" value="enable" />';

    name = meta_key + []
    id = ??? no idea, there’s no id parameter for the checkboxes
    value = value in text

    Can someone confirm what we’re supposed to put for hidden fields parameters?

    Thanks!

    Thread Starter ftpwp

    (@ftpwp)

    Ok, this 2nd method works like a charm in the specific case of user registrations.
    And the answers to my questions are:

    – name: indeed, it should be the field meta_key + “[]” for checkboxes otherwise the value will be saved in plain text and not in the Ultimate Member specific format
    – id: it looks like this parameter is useless in this specific case; we are not addressing anywhere for HTML fields
    – value: yes, it’s the value in text (no TRUE|FALSE, YES|NO, 1|0, etc.), BUT… be careful to have the functions.php file in UTF8 if there are special characters in the text of the value (like French accents: “Activé”)

    So lines like that are working fine for checkboxes:
    echo '<input type="hidden" name="profile_notif_new_mention[]" value="enable" />';

    Then I’ll mark this topic as “resolved” for the specific case of user’s registration, but I still need to clarify the 1st option for another context. But for that, I’ll open a new topic, it will be more clear.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Assign a value to a custom field?’ is closed to new replies.