Last active
February 6, 2023 15:53
-
-
Save kimwhite/1c277d1b80540cecc57f78401149f04e to your computer and use it in GitHub Desktop.
PMPro - Add Custom User Fields to the Admin Approval Email
This file contains hidden or 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 // 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