Support Forum » User Profile

Forum Replies Created

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • Andrea GuerrieroAndrea Guerriero
    Participant

    Hi Joie
    Just a custom function to check user role

    function awr_user_has_role ($user_id, $role) {
       if (is_numeric( $user_id))
         $user = get_user_by( 'id', $user_id );
       else
         $user = wp_get_current_user();
    
       if (empty($user))
         return false;
    
       return in_array($role, (array) $user->roles);
    }
    Andrea GuerrieroAndrea Guerriero
    Participant

    Here again just to post the final code to whom may be interested
    Obviously I had to check the doc to get it working because user_register pass a number while rwmb_profile_after_save_user pass an object

    function auto_login_new_user(  $user ) {
        $id = $user->user_id;
        wp_set_current_user($id);
        wp_set_auth_cookie($id);
        if(awr_user_has_role($id, 'subscriber')){
          wp_redirect("/proponi-un-luogo/");
        }
        exit();
    }
    add_action( 'rwmb_profile_after_save_user', 'auto_login_new_user' );
    Andrea GuerrieroAndrea Guerriero
    Participant

    Grazie mille! 🙂
    Long you’re an angel… and I’m quite ashamed of my laziness… I should definitely pay more attention to the documentation. There’s already everything in it. Indeed is probably the best api documentation I ever used: clear, accurate, complete, in depth, useful. I love your great work every day more.

    Andrea GuerrieroAndrea Guerriero
    Participant

    Hi Long
    Similar problem here
    I followed your link and the autologin works well
    But the extra fields I added in the registration form (custom and regular) are not saved anymore
    Well, somehow it saves only the user_url
    Before adding the autologin function data were all saved as aspected
    It seems I'm using the wrong WP hook and the autolign fires before META plugin complete it's work
    Any clues?

    Here's my code:

    add_filter( 'rwmb_meta_boxes', 'agri_new' );
    function agri_new( $meta_boxes ) {
        $prefix = '';
    
        $meta_boxes[] = [
            'title'  => __( 'Submitter', 'agri' ),
            'id'     => 'user-extra',
            'type'   => 'user',
            'fields' => [
                [
                    'name'     => __( 'Nome', 'agri' ),
                    'id'       => $prefix . 'first_name',
                    'type'     => 'text',
                    'required' => true,
                ],
                [
                    'name'     => __( 'Cognome', 'agri' ),
                    'id'       => $prefix . 'last_name',
                    'type'     => 'text',
                    'required' => true,
                ],
                [
                    'name'     => __( 'Telefono', 'agri' ),
                    'id'       => $prefix . 'user_phone',
                    'type'     => 'text',
                    'required' => true,
                ],
                [
                    'name' => __( 'Sito web', 'agri' ),
                    'id'   => $prefix . 'user_url',
                    'type' => 'url',
                ],
            ],
        ];
    
        return $meta_boxes;
    }

    Here's the custom function:

    function auto_login_new_user( $user_id ) {
        wp_set_current_user($user_id);
        wp_set_auth_cookie($user_id);
        if(awr_user_has_role(get_current_user_id(), 'subscriber')){
          wp_redirect("/proponi-un-luogo/");
        }
        exit();
    }
    add_action( 'user_register', 'auto_login_new_user' );
    in reply to: Admin Extensions Page Settings #28654
    Andrea GuerrieroAndrea Guerriero
    Participant

    Thanks Nguyen
    Actually is the first time for me. We buy the AIO versions because we develope a lot on WordPress and it seems the perfect solution for most of our recurring scenarios. There are so many options and docs to browse that problably I got confused. By the way thanks, I manage it with the MB Builder just like you suggest

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