admin管理员组

文章数量:1428474

I want to use this as a shortcode to show in any post/page

<?php 
    $user_id = get_current_user_id();
    if ($user_id == 0) {
        echo 'You are currently not logged in.';
    } else {
        echo 'You are logged in as user '.$user_id;
    }
?>

when I do this

// Add Shortcode
function uniqueID_shortcode() {

    $user_id = get_current_user_id();
    if ($user_id == 0) {
        echo 'You are currently not logged in.';
    } else {
        echo 'You are logged in as user '.$user_id;
    }

}
add_shortcode( 'naveen', 'uniqueID_shortcode' );

Gutenberg through an error of Updating failed, how to solve this? Thank you in advance.

I want to use this as a shortcode to show in any post/page

<?php 
    $user_id = get_current_user_id();
    if ($user_id == 0) {
        echo 'You are currently not logged in.';
    } else {
        echo 'You are logged in as user '.$user_id;
    }
?>

when I do this

// Add Shortcode
function uniqueID_shortcode() {

    $user_id = get_current_user_id();
    if ($user_id == 0) {
        echo 'You are currently not logged in.';
    } else {
        echo 'You are logged in as user '.$user_id;
    }

}
add_shortcode( 'naveen', 'uniqueID_shortcode' );

Gutenberg through an error of Updating failed, how to solve this? Thank you in advance.

Share Improve this question edited May 4, 2019 at 1:16 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 3, 2019 at 18:28 naveenkharwarnaveenkharwar 234 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

From add_shortcode

...Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results...

Try changing the echos in your callback to returns and see, if that helps.

本文标签: WordPress User ID as Shortcode