Created
April 22, 2019 10:18
-
-
Save femiyb/9e9fdeecf2da6c08b614fc8337da7de7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Add WP User Avatar from Register Helper field during checkout. | |
*/ | |
function my_updated_user_meta($meta_id, $user_id, $meta_key, $meta_value) { | |
// Change user_avatar to your Register Helper file upload name. | |
if( 'user_avatar' == $meta_key) { | |
$filename = $meta_value['fullpath']; | |
$filetype = wp_check_filetype( basename( $filename ), null ); | |
$wp_upload_dir = wp_upload_dir(); | |
$attachment = array( | |
'post_mime_type' => $filetype['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), | |
'post_status' => 'inherit' | |
); | |
$attach_id = wp_insert_attachment( $attachment, $filename); | |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
update_user_meta($user_id, 'wp_user_avatar', $attach_id); | |
} | |
} | |
add_action('added_user_meta', 'my_updated_user_meta', 10, 4); | |
// REGISTER HELPER | |
function my_pmprorh_init() | |
{ | |
//don't break if Register Helper is not loaded | |
if(!function_exists( 'pmprorh_add_registration_field' )) { | |
return false; | |
} | |
$fields = array(); | |
$fields[] = new PMProRH_Field( | |
'gender', // input name, will also be used as meta key | |
'select', // type of field | |
array( | |
'required' => true, // make this field required | |
'options' => array( // <option> elements for select field | |
'' => '', // blank option - cannot be selected if this field is required | |
'Male' => 'Male', // <option value="lessthan2000">< $2,000</option> | |
'Female' => 'Female', // <option value="2000to5000">$2,000-$5,000</option> | |
) | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
'birth_date', // | |
'date', // type of field | |
array( | |
'label' => 'Birth Date:', // custom field label | |
'required' => true, // make this field required | |
'profile' => true, // show in user profile | |
'memberslistcsv' => true, // Uncomment to Include in Member List CSV Export | |
// 'addmember' => true, // Uncomment to display in Add Member | |
) | |
); | |
$fields[] = new PMProRH_Field( | |
"user_avatar", // input name, will also be used as meta key | |
"file", // type of field | |
array( | |
"label"=>"Member Photo", | |
'required' => true, // make this field required | |
"profile"=>false, // show in user profile | |
)); | |
//add the fields into a new checkout_boxes are of the checkout page | |
foreach($fields as $field) | |
pmprorh_add_registration_field( | |
'checkout_boxes', // location on checkout page | |
$field // PMProRH_Field object | |
); | |
//that's it. see the PMPro Register Helper readme for more information and examples. | |
} | |
add_action( 'init', 'my_pmprorh_init' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment