Skip Navigation

[Resolved] Custom code doesn't work when using Cred hook

This support ticket is created 4 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by RensV5812 4 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#1610729

Hi guys,

I've written some custom code that generates an image based on the first letter of the name of a user that is registering and inserts that image into a custom user field called 'profielfoto'. When I run the code without the cred hook it works great, but when using the cred hook it doesn't. It updates the user profile picture but the picture itself isn't generated, so the result is a broken image icon.

I've triple checked the form id's.

add_action( 'cred_save_data', 'auto_profile_picture', 10, 2 );
function auto_profile_picture( $new_user_id, $form_data ){
  
    if ( $form_data['id'] == 3263 Or $form_data['id'] == 344 ) { // Edit as required
		
		$upload_dir = '../wp-content/uploads/auto-letter-profile-pictures';
		$filename = 'auto-profilepicture';

		// get new user_id
		$user_id = $new_user_id;
		
		// -----------------------------------------------------------------------------------------
		// --------------------------- Generate dynamic profile picture ----------------------------
		// -----------------------------------------------------------------------------------------

		// grab first letter of first name for default profile picture
		$first_name = get_user_meta( $user_id, 'first_name', true );
		$first_letter = $first_name[0];

		// rgb color values, add more if needed
		$color_array = array(
			// yellow
			array(252, 186, 3),
			// red
			array (245, 81, 66),
			// blue
			array(66, 170, 245),
			// green
			array(97, 199, 95),
			// orange
			array(240, 156, 53),
			// purple
			array(181, 53, 240),
			// black
			array(0, 0, 0)
		);

		$random_color = rand ( 0 , count($color_array) - 1 );

		// update uploadfile name
		$uploadfile = $upload_dir . '/' . $filename . '-' . $first_letter . $random_color . '.png';

		$im = @imagecreate(100, 100)
			or die("Cannot Initialize new GD image stream");

		// grab font
		// use different font? upload .ttf to: public_html/wp-content/themes/astra/assets/fonts
		$font = '../wp-content/themes/astra/assets/fonts/OpenSans-Bold.ttf';

		// create colors, first allocation automatically fills background
		$bg_color = imagecolorallocate($im, $color_array[$random_color][0], $color_array[$random_color][1], $color_array[$random_color][2]);
		$white = imagecolorallocate($im, 255, 255, 255);

		// write text
		imagettftext($im, 40, 0, 31, 70, $white, $font, $first_letter);

		imagepng($im, $uploadfile);

		// -----------------------------------------------------------------------------------------
		// ---------------------------- Add image to WordPress library -----------------------------
		// -----------------------------------------------------------------------------------------

		$wp_filetype = wp_check_filetype(basename($filename), null );

		$attachment = array(
			'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
			'post_mime_type' => $wp_filetype['type'],
			'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
			'post_content' => '',
			'post_status' => 'inherit',
		); 

		// insert new image into wordpress library
		$attach_id = wp_insert_attachment( $attachment, $uploadfile);

		// transfer image data (not enitrely sure how this works)
		require_once( ABSPATH . 'wp-admin/includes/image.php' );
		$attach_data = wp_generate_attachment_metadata( $attach_id, $uploadfile);
		wp_update_attachment_metadata( $attach_id,  $attach_data );

		// source for adding image to wordpress library: https://wordpress.stackexchange.com/questions/256830/programmatically-adding-images-to-media-library

		// -----------------------------------------------------------------------------------------
		// ----------------------------- Update users profile picture ------------------------------
		// -----------------------------------------------------------------------------------------

		// update user's profile picture
		update_user_meta( $user_id, 'wpcf-profielfoto',  wp_get_attachment_url($attach_id ) );
		update_user_meta( $user_id, 'last_name',  'vis'); // just for checking if $user_id worked
	}
}

What could be causing this?

Rens

#1611863

Hi Rens,

Thank you for contacting us and I'd be happy to assist.

The best way to troubleshoot and debug custom functions with multiple parts is to use a Tool like "WP PHP Console" plugin and inserting debugging checkpoints and outputs at different logical steps.
( ref: hidden link )

This will help you in narrowing down to the specific part or line of code that is failing or returning unexpected results/data.

regards,
Waqar

#1612297

I fixed the problem by changing the paths. I used getcwd(); to get the root folder path and then it worked. Thanks for the help. 🙂

My issue is resolved now. Thank you!