Skip to:
Content
Pages
Categories
Search
Top
Bottom

Avatar upload on sign up, s2members


  • ouegy
    Participant

    @ouegy

    Hi,

    I’m trying to include an avatar upload field within an s2Member pro form to handle user sign up.

    I have a custom form template for s2members which includes the following custom field:

    <label for="avatar_upload">Photo Upload: *</label>
    <input type="file" id="avatar_upload" name="avatar_upload">

    I’m having trouble then storing that data when the form is submitted. The code I’m trying is:

    
    add_action('ws_plugin__s2member_during_configure_user_registration', 'store_my_custom_input_fields');
    function store_my_custom_input_fields($vars = array())
    	{
    		$user = $vars['user']; // A WP_user object instance.
    		$_p = stripslashes_deep($_POST); // $_POST vars via form submission.
    		
    		if(!empty($_p['avatar_upload']) && is_string($_p['avatar_upload']))
    			update_user_option($user->ID, 'avatar_upload', esc_html($_p['avatar_upload']));
    		
    	}

    Any help is appreciated!

    Thanks,

    Ian

Viewing 12 replies - 1 through 12 (of 12 total)

  • Henry Wright
    Moderator

    @henrywright

    Hi @ouegy

    How are you saving the actual file to your file system?


    ouegy
    Participant

    @ouegy

    Hi @henrywright

    It’s currently being saved to wp_usermeta and is associated with the user.

    the meta_key is wp_avatar_upload and the meta_value image.jpg

    Where does buddypress store the avatars?


    ouegy
    Participant

    @ouegy

    Just checking the DB, all meta_values are longtext and I cant change to BLOB for that specific row…

    Is there some way I might be able to use the code behind the form on the /change-avatar/ page and store whatever file is uploaded in the correct table for BuddyPress?


    Henry Wright
    Moderator

    @henrywright

    There’s a lot more to uploading an avatar than just saving the meta. Check out xprofile_screen_change_avatar() to see how BuddyPress handles the process.

    Along with saving meta info, you’ll need to handle the actual file and save it to your file system (just as BuddyPress does). Then there’s cropping to think about too.

    Hope this info helps.


    ouegy
    Participant

    @ouegy

    Thanks I’ll take a look at that


    ouegy
    Participant

    @ouegy

    Hi @henrywright

    I’ve taken a slightly different approach as I couldn’t figure out customising the s2member form.

    What now happens is the user registers and is assigned a restricted membership level (level_1) and automatically logged in and redirected to /profile/change-avatar.

    At the point that the user clicks

    <input type="submit" name="avatar-crop-submit" id="avatar-crop-submit" value="<?php esc_attr_e( 'Crop Image', 'buddypress' ); ?>" />

    in change-avatar.php, is there some sort of event hook that could fire on this to change the user’s role eg. make them a full member (level_2)

    It’s a requirement that the user uploads a photo to become a member of the community.

    Thanks again


    Henry Wright
    Moderator

    @henrywright

    …is there some sort of event hook that could fire on this to change the user’s role eg. make them a full member (level_2)

    Yes. That’d be xprofile_avatar_uploaded. It fires immediately after the new avatar is processed.


    ouegy
    Participant

    @ouegy

    Thanks – I’m pretty new to hooks

    does this this look right?

    <?php
    
    add_action( 'xprofile_avatar_uploaded', 'update_membership_level' )
    
    function update_membership_level() {
    
    	$u = new WP_User( $user->ID );
            // Remove role
            $u->remove_role( 's2member_level1' );
    
            // Add role
            $u->add_role( 's2member_level2' );
    
    	}
    	
    ?>

    Henry Wright
    Moderator

    @henrywright

    I’m not using s2member so can’t test your function. However, I think you’re almost on the right track. One thing I noticed was your use of $user->ID. I don’t see where you’re getting the $user object from? You might need to use get_current_user_id() instead. Try:

    <?php
    
    add_action( 'xprofile_avatar_uploaded', 'update_membership_level' );
    
    function update_membership_level() {
    
    	$u = new WP_User( get_current_user_id() );
            // Remove role
            $u->remove_role( 's2member_level1' );
    
            // Add role
            $u->add_role( 's2member_level2' );
    
    	}
    	
    ?>

    Henry Wright
    Moderator

    @henrywright

    Also, if you wanted to condense your function into less statements, you could use the set_role() method instead of remove_role() and add_role(). See here


    ouegy
    Participant

    @ouegy

    Thanks Henry that works perfectly!

    The support on here is amazing!


    Henry Wright
    Moderator

    @henrywright

    @ouegy you wrote the function, so the credit is all yours! 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Avatar upload on sign up, s2members’ is closed to new replies.
Skip to toolbar