最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WordPress User ID as Shortcode

programmeradmin1浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论