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