简体   繁体   中英

How to customize comment functionality in wordpress?

I am new to wordpress and i want to approve the comment programmatically whenever any user comment on blog post,so i have created plugin and i am using this code:

add_filter('wp_insert_comment','sandy_approve_comment');

function sandy_approve_comment($comment_id) {

    // Get the comment based on the incoming ID
    $comment = get_comment( $comment_id );

    // Set its approved comment to 1
    $comment->comment_approved = 1;

    // Save this value to the database
    wp_update_comment( $comment );

}

but i am getting this error: Cannot use object of type stdClass as array in /var/www/html/SandyBlog/wp-includes/comment.php on line 1915

I know we can do this from admin panel but i want to do it programmatically.Please suggest me what to do. Thanks in advance

Are you try to automatically approve every comment without moderating them? If so just uncheck the following settings under Settings > Discussion

  • Comment must be manually approved
  • Comment author must have a previously approved comment

Try this code :

// Get the comment object
$comment = get_comment( $comment_id );

// Get the comment object
$comment_id = $_comment->comment_ID;

// Set its to not approve comment
wp_set_comment_status($comment_id, '0');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM