Forked from ipokkel/my-pmprorh-extra-fields-admin-email.php
Created
September 29, 2022 17:05
-
-
Save MaryOJob/c9fedf42a532f8b4069e3227970034be to your computer and use it in GitHub Desktop.
Add Register Helper fields to the admin checkout notification email.
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 Custom Registration Fields and their values to the Admin Checkout Notification 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_pmprorh_add_extra_fields_to_admin_email( $email ) { | |
global $wpdb; | |
//only update admin confirmation emails | |
if ( ! empty( $email ) && strpos( $email->template, 'checkout' ) !== 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['user_email'] . "' LIMIT 1" ); | |
if ( ! empty( $user_id ) ) { | |
//get meta fields | |
global $pmprorh_registration_fields; | |
$profile_fields = array(); | |
if ( ! empty( $pmprorh_registration_fields ) ) { | |
//cycle through groups | |
foreach ( $pmprorh_registration_fields as $where => $fields ) { | |
//cycle through fields | |
foreach ( $fields as $field ) { | |
if ( ! is_a( $field, 'PMProRH_Field' ) || ! pmprorh_checkFieldForLevel( $field, 'profile', $user_id ) ) { | |
continue; | |
} | |
if ( ! empty( $field->profile ) ) { | |
$profile_fields[] = $field; | |
} | |
} | |
} | |
} | |
unset( $fields ); | |
$fields = $profile_fields; | |
//add to bottom of email | |
if ( ! empty( $fields ) ) { | |
$email->body .= '<p>Extra Fields (Admin) :<br />'; | |
foreach ( $fields as $field ) { | |
if ( ! is_a( $field, 'PMProRH_Field' ) ) { | |
continue; | |
} | |
$email->body .= '- ' . $field->label . ': '; | |
$value = get_user_meta( $user_id, $field->meta_key, true ); | |
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; | |
} | |
function swop_pmprorh_pmpro_email_filter_extra_fields() { | |
// Let's only do this if Register Helper is active | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return; | |
} | |
remove_filter( 'pmpro_email_filter', 'pmprorh_pmpro_email_filter' ); | |
add_filter( 'pmpro_email_filter', 'my_pmprorh_add_extra_fields_to_admin_email', 10, 2 ); | |
} | |
add_action( 'init', 'swop_pmprorh_pmpro_email_filter_extra_fields' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment