I’ve created a plugin that with a shortcode displays all users with the custom role “ansatt”. This works perfectly fine, but I’d like to be able to set the role as a parameter within the shortcode.
This is the what I want to achieve: [hw_display_users role=”ansatt”]
Below is the function I’ve created. bottom line is that I’m trying to move the 'role' => 'ansatt' into the actual shortcode so that the role can easily be altered.
How on earth do I accomplish this?
function display_user_roles(){
$args = array(
'role' => 'ansatt',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
ob_start();
echo '<div class="owl-carousel owl-theme">';
foreach ( $users as $user ) {
echo '<div class="item"><a href="' . get_author_posts_url( $user->ID, $user->user_nicename ) . '">';
$image1 = get_field('profilbilde', $user);
echo '<div class="hw-thumb">';
echo '<img class="profile1" src="';
echo $image1['url'];
echo '"/>';
echo '</div>';
echo '<div class="hw-stilling">'. get_field('stilling', $user) . '</div>';
echo '<h2 class="hw-username">'. esc_html( $user->display_name ) . '</h2>';
echo '<div class="hw-epostadresse">'. get_field('epostadresse', $user) . '</div>';
echo '</a></div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'hw_display_users', 'display_user_roles' );
Edit – working code
function display_user_roles( $atts ){
$args = array(
'orderby' => 'user_nicename',
'order' => 'ASC'
);
if ( !empty( $atts['role'] ) )
$args['role'] = $atts['role'];
$users = get_users( $args );
ob_start();
echo '<div class="owl-carousel owl-theme">';
foreach ( $users as $user ) {
echo '<div class="item"><a href="' . get_author_posts_url( $user->ID, $user->user_nicename ) . '">';
$image1 = get_field('profilbilde', $user);
echo '<div class="hw-thumb">';
echo '<img class="profile1" src="';
echo $image1['url'];
echo '"/>';
echo '</div>';
echo '<div class="hw-stilling">'. get_field('stilling', $user) . '</div>';
echo '<h2 class="hw-username">'. esc_html( $user->display_name ) . '</h2>';
echo '<div class="hw-epostadresse">'. get_field('epostadresse', $user) . '</div>';
echo '</a></div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'hw_display_users', 'display_user_roles' );
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
First, you’ll have to get shortcode parameter(s). Change function’s declaration to:
function display_user_roles( $atts ) {
Your shortcode will be either [hw_display_users] or [hw_display_users role="roletopass"]. The first (no parameter) will use role specified in $args array. Second will replace $args->role with parameter passed. Add
if ( !empty( $atts['role'] ) )
$args['role'] = $atts['role'];
before
$users = get_users( $args );
Remaining code should be ok.
For more information how to handle shortcodes with parameters read this.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0