Passing get_users attributes to shortcode atts
-
Hello,
I built the following function to show a list of my users based on the parameters set in the get_users array. And it works.
$blogusers = get_users( array( 'role' => 's2member_level4', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'last_name', 'include' => array(), 'exclude' => array(745), 'meta_query' => array( 'key' => 'wp_s2member_custom_fields', 'value' => 'Senior Management', 'compare' => 'LIKE' ) ) ); foreach ( $blogusers as $user ) { $s2user = get_user_meta( $user->ID, 'wp_s2member_custom_fields', true ); $department = get_user_field('gs_department', $user->ID); echo '<div class="stafflisting staff-'.esc_html($user->ID).'">'; echo get_wp_user_avatar($user->ID); echo '<strong style="text-transform: uppercase;">' . esc_html( $user->last_name ) . '</strong> '; echo esc_html( $user->first_name ) . '<br />'; if(qtrans_getLanguage()=='en'): echo '<div class="user_meta">' . $s2user['gs_title_eng'] . '</div>'; endif; if(qtrans_getLanguage()=='de'): echo '<div class="user_meta">' . $s2user['gs_title_de'] . '</div>'; endif; if(is_array($department)) { echo '<div class="dept user_meta">'; if(qtrans_getLanguage()=='en'): echo 'Dept: '; endif; if(qtrans_getLanguage()=='de'): echo 'Abt: '; endif; foreach($department as $value) $value = reset ($value); $department = implode(' / ', $department); echo $department; }; echo '</div>'; echo '<div class="user_contact">'; echo '<a href="mailto:' . esc_html( $user->user_email ) . '"><i class="fa fa-envelope-o" aria-hidden="true"></i> Email</a></div>'; if (($s2user['gs_direct_line'])!==null) { echo 'Direct Line: ' . $s2user['gs_direct_line']; }; echo '</div>'; }
What I would very much like to do is convert this into a shortcode in which I could pass the get_users array to the shortcode atts, such as
[userdata role="s2member_level4" order="ASC" orderby="meta_value" meta_key"last_name"]
and so far I have not succeeded.
Here is the code that I am testing one or two fields with, which does not work. The specified atts in the shortcode, like in the example above, are not picked up.
function getUserData_func( $atts ) { $user_info = get_users( $atts[array( 'role' => 's2member_level4', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'last_name', 'include' => array(), 'exclude' => array() )]); foreach ( $user_info as $user ) { echo '<strong style="text-transform: uppercase;">' . esc_html( $user->last_name ) . '</strong> '; echo esc_html( $user->first_name ) . '<br />'; } } add_shortcode( 'userdata', 'getUserData_func' );
Can anyone point me in the right direction?
Very much appreciated.
- The topic ‘Passing get_users attributes to shortcode atts’ is closed to new replies.