admin管理员组

文章数量:1297131

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' );

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' );
Share Improve this question edited Apr 12, 2021 at 11:00 André Giæver asked Apr 8, 2021 at 9:02 André GiæverAndré Giæver 32 bronze badges 1
  • Are you asking how shortcode attributes work? – Tom J Nowell Commented Apr 8, 2021 at 9:38
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: pluginsCustom shortcode for displaying user based on a role parameter