Creating Specific Custom Avatars with Gravity Forms (2 posts)

  1. megart04
    Posted 1 year ago #

    This is a solution to a problem I was trying to solve for years. A client who has a Buddypress site wanted me to create a custom set of avatars for their site that would be the only avatars allowed for use on the site. They did not want people uploading their own avatars or using gravatars.

    Recently we switched to Gravity Forms which offers a Buddypress Registration extra/plugin (if you purchase their developers license). With this we could edit our registration form to add radio buttons that contained images. A user could select which image/avatar they wanted and later we would put it in manually.

    Then we talked to Gravity Forms and one of their support people agreed to write the code for us that would take those image/avatars and make them appear automatically in the new user profile. I was given permission to share this.

    Things you need to do before starting: Install Gravity Forms, install Buddypress Registration, create a registration form and add a field for radio buttons.

    1. Radio Buttons. The way you want to create these in the Gravity Form is to put the img src file in the label field. Switch on the value field and here is where you put the avatars name. Take Note: You will need to know your Form ID later (found at the top below the words "Form Editor" and your Field ID later (found at the top of the radio buttons field you're using).

    2. You need to create two image files for your avatars - a folder of "bpthumb" and "bpfull". bpthumb must be 50x50 pixels and bp-full are 150x150 pixels. When naming them they'll need to have the -bpfull or -bpthumb extensions. Example: "avatarname-bpfull.jpg". Using FTP place these folders in wp-content/uploads/avatars

    3. So the last bit is this below code that goes in your functions.php
    You'll need to change two numbers in two places in this code to customize it for yourself - Form ID and Field ID. In the below example Form ID is 75 and Field ID is 9. Replace appropriately.

    // applies to form 75 only,  Change 75 here to your form ID. applies to field ID 9 only, change 9 to your field ID.
    add_action('gform_user_registered', 'add_avatar_files', 10, 4);
    function add_avatar_files($user_id, $config, $entry, $user_pass) {
    	if ($entry['form_id'] == 75) {
    		GFCommon::log_debug("entry object for form #{$entry['form_id']}, entry #{$entry['id']}:");
    		GFCommon::log_debug(print_r($entry, true));
    		GFCommon::log_debug("This is user ID: {$user_id}");
    		// where are the avatars stored?
    		$upload_dir = wp_upload_dir();
    		$avatars_directory = $upload_dir['basedir'] . '/avatars';
    		$avatar_thumb_dir = $avatars_directory . '/bpthumb';
    		$avatar_full_dir = $avatars_directory . '/bpfull';
    		GFCommon::log_debug("Upload directory:");
    		GFCommon::log_debug(print_r($upload_dir, true));
    		GFCommon::log_debug("Avatars directory: {$avatars_directory}");
    		GFCommon::log_debug("Avatar thumbnmnail directory: {$avatar_thumb_dir}");
    		GFCommon::log_debug("Avatar full directory: {$avatar_full_dir}");
    		// filenames
    		$file_base = $entry['9'];
    		$thumb = $file_base . '-bpthumb.jpg';
    		$full = $file_base . '-bpfull.jpg';
    		GFCommon::log_debug("Filename base from the entry: {$file_base}");
    		GFCommon::log_debug("Thumbnail filename: {$thumb}");
    		GFCommon::log_debug("Full avatar filename: {$full}");
    		// make an avatar directory for tne newly registered user
    		$user_avatar_directory = $avatars_directory . '/' . $user_id;
    		GFCommon::log_debug("Try to create a directory for user avatars:\n{$user_avatar_directory}");
    		if(!wp_mkdir_p($user_avatar_directory)) {
    			// log the failure to create the user's avatar directory
    			GFCommon::log_debug("Failed to create directory:\n{$user_avatar_directory}");
    		else {
    			GFCommon::log_debug("Successfully created directory:\n{$user_avatar_directory}");
    		// copy the thumbnail
    		$original_thumb_file = $avatar_thumb_dir . '/' . $thumb;
    		$user_thumb_file = $user_avatar_directory . '/' . $thumb;
    		if (!copy($original_thumb_file, $user_thumb_file)){
    			// log the failure to create the user thumbnail
    			$errors = error_get_last();
    			GFCommon::log_debug("Failed to copy\n{$original_thumb_file}\nto\n{$user_thumb_file}:");
    			GFCommon::log_debug("PHP error: {$error['message']}");
    		else {
    			// log success
    			GFCommon::log_debug("Successfully copied\n{$original_thumb_file}\nto\n{$user_thumb_file}");
    		// copy the full avatar
    		$original_full_file = $avatar_full_dir . '/' . $full;
    		$user_full_file = $user_avatar_directory . '/' . $full;
    		if (!copy($original_full_file, $user_full_file)) {
    			// log the failure to create the user full avatar
    			$errors = error_get_last();
    			GFCommon::log_debug("Failed to copy\n{$original_full_file}\nto\n{$user_full_file}:");
    			GFCommon::log_debug("PHP error: {$error['message']}");
    		else {
    			// log success
    			GFCommon::log_debug("Successfully copied\n{$original_full_file}\nto\n{$user_full_file}");

    That's it. This should work.

  2. arosenkoetter
    Posted 1 year ago #

    Hmm, this is exactly what I was looking for. But, when I added this to my functions.php file in my theme, it killed the whole WP instance. Everything just went to a white page (I mean everything - couldn't get to wp-admin either). I removed it from the functions file and everything was back. I'm not seeing an error.

    Any idea what could be the issue?

Topic Closed

This topic has been closed to new replies.

About this Topic