Forked from ipokkel/my-pmprorh-extra-fields-admin-email.php
Last active
June 27, 2023 17:16
-
-
Save kimwhite/e1b2ced424f0b1b8f9102dd50136179e to your computer and use it in GitHub Desktop.
Add Register Helper fields to the admin checkout notification 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 | |
/** | |
* Add User 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 $pmpro_user_fields; | |
$profile_fields = array(); | |
if ( ! empty( $pmpro_user_fields ) ) { | |
//cycle through groups | |
foreach ( $pmpro_user_fields as $where => $fields ) { | |
//cycle through fields | |
foreach ( $fields as $field ) { | |
if ( ! pmpro_is_field( $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 ( ! pmpro_is_field( $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( 'pmpro_add_user_fields_to_email' ) ) { | |
return; | |
} | |
remove_filter( 'pmpro_email_filter', 'pmpro_add_user_fields_to_email' ); | |
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