• Resolved ftpwp

    (@ftpwp)


    Hi,

    I’ve added a checkbox in the UM profile, called profile_notification.
    Then I’ve created a UM custom role called no_notification.

    And I would like to know how to automatically add the no_notification role to a user if in his profile the checkbox is unticked and remove this no_notification role if the checkbox is ticked?

    I got inspired by the bottom of this article (Programmatically apply a custom user role)…
    https://usersinsights.com/wordpress-custom-role/
    …but I need to adapt the code according to how I can get the UM profile checkbox value.

    Any idea?
    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter ftpwp

    (@ftpwp)

    I would need something like that and I need help for the 3 “?????”…

    function ui_profile_notification_check( ????? ) {

    // Retrieve “profile_notification” checkbox value
    $profile_notification = ?????;

    // Check if “profile_notification” is ticked
    if ( $profile_notification = True ) {

    // If ticked, add the ‘no_notification’ role to the user
    $user->add_role(‘no_notification’);
    }
    else {
    // If unticked, remove the ‘no_notification’ role to the user
    $user->remove_role(‘no_notification’);
    }
    }
    add_action(‘?????’, ‘ui_profile_notification_check’, 10, 2);

    And regarding the last ?????, I would like the add_action() to take place when the user save his profile form.

    Thanks for your help!

    Thread Starter ftpwp

    (@ftpwp)

    I’ve probably found the right hooks: um_user_register right after registration according to what they ticked and um_user_edit_profile right after they edit their profile.
    Correct?

    Thread Starter ftpwp

    (@ftpwp)

    Ooops. add_role() & remove_role() are not the right functions.
    I want to set a value for the “Ultimate Member Role” which adds a second role to the user (on top of the WP one).

    Thread Starter ftpwp

    (@ftpwp)

    Ok, it’s progressing…

    add_action(‘um_user_after_updating_profile’, ‘ui_profile_notification_check’, 10, 1);

    function ui_profile_notification_check( $submitted ) {

    // Check if “profile_notification” is ticked
    if ( isset($submitted[‘profile_notification’]) ) {

    // If ticked, assign the ‘no_notification’ role to the user
    ???? How-to assign the “no_notification” role as Ultimate Member Role?

    }
    else {
    // If unticked, remove the ‘no_notification’ role to the user
    ???? How-to remove the “no_notification” role as Ultimate Member Role?
    }
    }

    Hook is correct.
    Test of the checkbox is correct.

    I just need to find how to assign or remove a role as “Ultimate Member Role”, so on top of the WP role.
    BTW, the $submitted array does not provide the edited profile User ID. And it’s not necessarily the current user ID.

    Any idea how to do?

    Thread Starter ftpwp

    (@ftpwp)

    Finally, add_role() & remove_role() were the right functions!

    So I now only have one last issue: how-to identify which profile has just been updated, submitted?

    It’s not in the $submitted variable and if we add the login or email on the profile form, it’s never displayed in edit mode so never added in the $submitted variable ??

    Other than that, this code added in the functions.php of the theme works…

    add_action(‘um_user_after_updating_profile’, ‘ui_profile_notification_check’, 10, 1);

    function ui_profile_notification_check( $submitted ) {

    // Check if “profile_notification” is ticked
    if ( isset($submitted[‘profile_notification’]) ) {

    // If ticked, add the ‘um_no_notification’ role to the user
    $user = ?????; // How-to identify the profile that has been updated/submitted?
    $user->remove_role(‘um_no_notification’);
    }
    else {
    // If unticked, remove the ‘um_no_notification’ role to the user
    $user = ?????; // How-to identify the profile that has been updated/submitted?
    $user->add_role(‘um_no_notification’);
    }
    }

    Thread Starter ftpwp

    (@ftpwp)

    I think the $user_id variable is missing in the um_user_after_updating_profile hook :-/

    If we take a similar hook like um_after_save_registration_details, it has $submitted AND $user_id! Otherwise, with only $submitted, so far I really don’t know how we can know which profile has just been updated ??

    Thread Starter ftpwp

    (@ftpwp)

    Fixed!

    $user_id variable added in the um_user_after_updating_profile hook in version 2.1.0…
    https://github.com/ultimatemember/ultimatemember/issues/579
    …and then I just needed to use the add_role() & remove_role() functions.

    Thread Starter ftpwp

    (@ftpwp)

    Closed

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How-to assign custom role according to profile checkbox?’ is closed to new replies.