jeudi 8 septembre 2016

Add conditional buttons with shortcode

I am trying to add a couple of buttons using a [button] shortcode. What I want to achieve is that the first button BUTTON-01 shows to all users, while the other button BUTTON-02 only shows to "author" user role.

Here is my code:

function shortcode_button() {

    global $current_user;
    get_currentuserinfo();

    // Show this button to ALL
    return '<a class="button" href="http://example.com/01">FOR ALL</a>';

    // Show button to AUTHORS ONLY
    if ( user_can( $current_user, "author" ) ) {
            return '<a class="button" href="http://example.com/02">FOR AUTHORS ONLY</a>';
        }

}
add_shortcode( 'button', 'shortcode_button' );

When logged in as AUTHOR, I can see the first button, but the second just doesn't show up.

Am I missing something? Should there be endif; statement maybe?

Aucun commentaire:

Enregistrer un commentaire