skip to Main Content

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

2

Answers


  1. basically you have to check the current user id by get_current_user_id()

    here is the code that i have modified. I think it will work for you.

    function my_shortcode_function() {
    ob_start();
    $jonny_id = 1; // You have to collect the specific user id from wp dashboard
    $sally_id = 2; // You have to collect the specific user id from wp dashboard
    $jamie_id = 3; // You have to collect the specific user id from wp dashboard
    
    if(is_user_logged_in()){
    
        if( $jonny_id == get_current_user_id() ){
            return reblex_display_block(610);
        }
        elseif( $sally_id == get_current_user_id() ){
            return reblex_display_block(199);
        }
        elseif( $jamie_id == get_current_user_id() ){
            return reblex_display_block(554);
        }
    }
    
    return ob_get_clean();
    }
    
    add_shortcode('coolshortcodename', 'my_shortcode_function');
    
    Login or Signup to reply.
  2. 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);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search