public
Last active

get custom user meta for email users plugin

  • Download Gist
user_meta.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?php
 
add_action( admin_head, 'return_meta' );
function return_meta() {
get_user_meta_field_data ( 'drmc-department' );
get_user_meta_field_data ( 'drmc-department', 'er' );
}
 
function get_users_by_meta_data( $meta_key, $meta_value ) {
// Query for users based on the meta data
$user_query = new WP_User_Query(
array( 'meta_key' => $meta_key, 'meta_value' => $meta_value )
);
// Get the results from the query, returning the first user
$users = $user_query->get_results();
return $users;
} // end get_users_by_meta_data
 
function get_user_meta_field_data( $user_meta_field, $user_meta_field_value=NULL ) {
$meta_field_values = array();
$user_meta_field_values = array();
foreach ( get_users_by_meta_data( $user_meta_field, $user_meta_field_value ) as $user ) {
$meta_field_values[] = get_user_meta( $user->ID, $user_meta_field );
//get emails for specified custom user meta field value
if ( ! is_null( $user_meta_field_value ) ) {
$emails[$user->display_name] = $user->user_email;
}
}
//get values for custom user meta field and return array
if ( is_null( $user_meta_field_value ) ) {
foreach ( $meta_field_values as $meta_field_value ) {
if ( ! in_array( $meta_field_value[0], $user_meta_field_values ) ) {
$user_meta_field_values[] = $meta_field_value[0];
}
}
rsort($user_meta_field_values);
$user_meta_field_values = array_filter($user_meta_field_values);
sort($user_meta_field_values);
print_r ( $user_meta_field_values );
return $user_meta_field_values;
}
//return array of email addresses for specific custom user meta field value
if ( ! is_null( $user_meta_field_value ) ) {
print_r ($emails);
return $emails;
}
}

I'm looking for what (I think) this code does. How do you use it with the Email Users plugin? We have a store with customers and subsets of those will have the same pickup address for their orders. We want to be able to email those customers by pickup location (which is stored in a user_meta field).

I wrote this as an example for getting the Emails Users plugin to grab meta data and use it to create a group mailing based upon that. This has Ben done for Email Users v4.4.4

Take a look at one of my other gists for the plugin used by Email Users to set this.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.