如何WordPress的帖子添加评论只为登录用户?

时间:2018-07-04 11:17:36

标签: php wordpress function comments hook

wordpress中的帖子如何仅为登录用户添加评论?

访问者必须回答自己的添加评论。但我需要发布信息,请发表评论(仅针对登录用户),用户必须注册到站点,然后才能添加评论发布

可以使用功能文件来做到这一点吗?

我发现有2个代码,但没有用,并且不知道如何使用它:

在面对链接中找到此内容: https://codex.wordpress.org/Function_Reference/wp_get_current_user

代码:

<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
} else {
    // Logged in.
}
?>

,在面对链接中找到以下内容: https://codex.wordpress.org/Function_Reference/comment_form

代码:

'<p class="must-log-in">' .  sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'

并结合这两者,我构建了以下功能!

<?php
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
    // Not logged in.
    '<p class="must-log-in">' .  sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
} else {
    // Logged in.
}
?>

并使用Insert PHP Code Snippet插件将其添加到我的帖子中,仅用于(a)个帖子

请帮助我做到这一点。谢谢大家

2 个答案:

答案 0 :(得分:0)

设置>讨论,您将找到选项:Users must be registered and logged in to comment。 -http://prntscr.com/k2f02d

选中此选项并保存将满足您的要求的设置。

答案 1 :(得分:0)

在菜单导航下查看

Settings >>> Discussion 
Checked the "Users must be registered and logged in to comment " checkbox