Search code examples
phpwordpresswordpress-gutenbergwordpress-shortcode

How can I create a shortcode based on the current Wordpress user?


I want to create a shortcode with conditions based on who the current user is I have the essence of my shortcode...

function my_shortcode_function() {
     return 'here is the content of my shortcode!';
}

add_shortcode('coolshortcodename', 'my_shortcode_function');

But I need to add conditions to it...I want to say if user ID is Jonny then show this, if the user is Sally show that, if the user is Jamie something else...

function my_shortcode_function() {
IF JONNY
     return reblex_display_block(610);
ELSE IF SALLY
     return reblex_display_block(199);
ELSE IF JAMIE
     return reblex_display_block(554);
}

add_shortcode('coolshortcodename', 'my_shortcode_function');

Solution

  • function my_shortcode_function() {
        $current_user_id = get_current_user_id();
        if ($current_user_id == 5) // where user ID of Jonny is 5
            return reblex_display_block(610);
        else if ($current_user_id == 6) // where user ID of Sally is 6
            return reblex_display_block(199);
        else if ($current_user_id == 7) // where user ID of Jamie is 7
            return reblex_display_block(554);
    }