Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/305bb58cca60bd13083831d9b65bab13 to your computer and use it in GitHub Desktop.
Save JarrydLong/305bb58cca60bd13083831d9b65bab13 to your computer and use it in GitHub Desktop.
PMPro - Add Custom User Fields to the Admin Approval Email
<?php // don't copy this line
/**
* Adds Custom User Fields to the Approvals Email.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_pmprorh_approval_email_filter( $email ) {
if ( ! function_exists( 'pmprorh_getProfileFields' ) ) {
return $email;
}
global $wpdb;
// only update admin confirmation emails
if ( ! empty( $email ) && strpos( $email->template, 'approval' ) !== false && strpos( $email->template, 'admin' ) !== false ) {
// get the user_id from the email
$user_id = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email = '" . $email->data['member_email'] . "' LIMIT 1" );
if ( ! empty( $user_id ) ) {
// get meta fields
$fields = pmprorh_getProfileFields( $user_id );
// add to bottom of email
if ( ! empty( $fields ) ) {
$email->body .= "<p>" . __( 'Extra Fields:', 'paid-memberships-pro' ) . "<br />";
foreach($fields as $field)
{
if( ! pmpro_is_field( $field ) ) {
continue;
}
$email->body .= "- " . $field->label . ": ";
$meta_value = get_user_meta($user_id, $field->meta_key, true);
if( ! empty( $meta_value ) ) {
$value = $meta_value;
} else {
$value = isset( $_REQUEST[$field->meta_key] ) ? sanitize_text_field( $_REQUEST[$field->meta_key] ) : '';
}
if($field->type == "file" && is_array($value) && !empty($value['fullurl']))
$email->body .= $value['fullurl'];
elseif(is_array($value))
$email->body .= implode(", ", $value);
else
$email->body .= $value;
$email->body .= "<br />";
}
$email->body .= '</p>';
}
}
}
return $email;
}
add_filter( 'pmpro_email_filter', 'my_pmpro_pmprorh_approval_email_filter', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment