user1481153
user1481153

Reputation: 169

Wordpress. How to use shortcode with if else statement?

I have following code but it's displaying shortcode in text rather than showing results of shortcode.

$curr_user_id = get_current_user_id();
// the value is 0 if the user isn't logged-in
if ( $curr_user_id != 0 ) {
    // we know now the user is logged-in, so we can use his/her ID to get the user meta
    $um_value = get_user_meta( $curr_user_id, 'user_phone', true );
    // now we check if the value is correct
    if ( ! empty( $um_value ) && $um_value == 'French' ) {
        // if so we can output something
        echo '[ld_course_list course_category_name="french" col=4 progress_bar=true]';
    } else {
    // else the code, eg.
    echo '[ld_course_list course_category_name="english" col=4 progress_bar=true]';
}
}

Above code will result in text below rather than showing results of the shortcode

[ld_course_list course_category_name="english" col=4 progress_bar=true]

How do I change my code so it actually runs shortcode?

Thank you.

Following code worked. Only problem is do_shortcode is breaking stylesheet. Anyone know why or how to fix it?

<?
$curr_user_id = get_current_user_id();
// the value is 0 if the user isn't logged-in
if ( $curr_user_id != 0 ) {
    // we know now the user is logged-in, so we can use his/her ID to get the user meta
    $um_value = get_user_meta( $curr_user_id, 'user_phone', true );
    // now we check if the value is correct
    if ( ! empty( $um_value ) && $um_value == 'French' ) {
        // if so we can output something
        echo do_shortcode('[ld_course_list course_category_name="french" col=4 progress_bar=true]');
    } else {
    // else the code, eg.
    echo do_shortcode('[ld_course_list course_category_name="english" col="4" progress_bar="true"]');
}
}
?>

Upvotes: 1

Views: 2733

Answers (1)

Atlas_Gondal
Atlas_Gondal

Reputation: 2552

You are using it in wrong way, the method you are trying is used inside wordpress pages, posts etc.

To use inside plugin, theme or any other .php file, use WordPress function called do_shortcode() that lets you add shortcodes directly in your themes and plugins.

Add it like this:

echo do_shortcode("[ld_course_list course_category_name="english" col=4 progress_bar=true]");

Upvotes: 2

Related Questions