how wordpress a post add comment only for logged u

2019-09-21 17:43发布

问题:

How a post in wordpress add comment only for logged users?

visitors must answer their own add in comments. but i need a post, leave a comment (only for logged users) and users must register to site beforce add comment to post

can use functions file to do it ?

i found that 2 code but not work and do not know how to use it:

in facing link find this: https://codex.wordpress.org/Function_Reference/wp_get_current_user

Code:

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

and in facing link find this: https://codex.wordpress.org/Function_Reference/comment_form

code:

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

and from the combination of these two together, I built below function!

<?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.
}
?>

and add this to my post with Insert PHP Code Snippet plugin for use only (a) post

please help me to do this. thank you all

回答1:

Settings > Discussion you will find the option: Users must be registered and logged in to comment. - http://prntscr.com/k2f02d

Checked this option and save the settings your requirement will be fulfilled.



回答2:

Review below menu navigation

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