Skip to content

Instantly share code, notes, and snippets.

@gogrw
Created May 13, 2019 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gogrw/b4f4b549955d56970ea32b33ba8af1e1 to your computer and use it in GitHub Desktop.
Save gogrw/b4f4b549955d56970ea32b33ba8af1e1 to your computer and use it in GitHub Desktop.
Shortcode for Listing Users from Meta Data - Passing Merge Tags Through Parameters
/**
* Shortcode for Listing Users from Meta Data
* Usage: [list_users key="{report_type:3}" value="{which_report:1}" userrole="{which_state:2}"]
*/
function users_from_meta($atts) {
$atts = shortcode_atts( array(
'key' => '',
'value' => '',
'userrole' => '',
), $atts );
$user_query = new WP_User_Query( array(
'meta_key' => $atts['key'],
'meta_value' => $atts['value'],
'role__in' => wp_parse_list( $atts['userrole'] ),
) );
$users = $user_query->get_results();
if (!empty($users)) {
$results = '<table class=list_user_table>
<tr>
<td class=list_user_title1>Shop #</td>
<td class=list_user_title2>City</td>
<td class=list_user_title3>State</td>
<td class=list_user_title4>Franchisee</td>
<td class=list_user_title5>Consultant</td>
</tr>';
foreach ($users as $user){
$results .= '<tr>
<td class=list_user_td_left><a href=https://clearstone.gogrw.com/user/shop' . $user->Store_Number . '/>' . $user->Store_Number . '</a></td>
<td class=list_user_td>' . $user->City . '</td>
<td class=list_user_td>' . $user->State . '</td>
<td class=list_user_td>' . $user->display_name . '</td>
<td class=list_user_td>' . $user->Group . '</td>
</tr>';
}
$results .= '</table>';
} else {
$results = 'No users found';
}
wp_reset_postdata();
return $results;
}
add_shortcode( 'list_users', 'users_from_meta' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment