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

comments - wp_new_comment requires author url and author email

programmeradmin0浏览0评论

I am using the wp_new_comment() (previously used the wp_insert_comment()) function.

I now notice that I get a

Notice: Undefined index: comment_author_url

and a:

Notice: Undefined index: comment_author_email

in the wp-includes/comment.php file.

Previously, when using wp_insert_comment() I left these fields in the arguments blank (ie: left them out of the arguments array) and I didn't receive any notices.

How come I am receiving a notice for these arguments now? Are these arguments required for the wp_new_comment() function but not for wp_insert_comment()? If so, why?

All of my comments are written by registered users. I can simply enter empty strings for these fields or use the user's registration information to populate the fields. Any suggestions regarding this?

(my comment form is taken from the plugin I'm using).

Thanks.

I am using the wp_new_comment() (previously used the wp_insert_comment()) function.

I now notice that I get a

Notice: Undefined index: comment_author_url

and a:

Notice: Undefined index: comment_author_email

in the wp-includes/comment.php file.

Previously, when using wp_insert_comment() I left these fields in the arguments blank (ie: left them out of the arguments array) and I didn't receive any notices.

How come I am receiving a notice for these arguments now? Are these arguments required for the wp_new_comment() function but not for wp_insert_comment()? If so, why?

All of my comments are written by registered users. I can simply enter empty strings for these fields or use the user's registration information to populate the fields. Any suggestions regarding this?

(my comment form is taken from the plugin I'm using).

Thanks.

Share Improve this question asked Oct 14, 2016 at 14:58 theyuvtheyuv 2091 silver badge15 bronze badges 2
  • How are you including your code when you write I'm using ? wp_new_comment need 1 argument. your array of data doesn't seems to be populated. – Benoti Commented Oct 14, 2016 at 16:34
  • I did not show any array of data, but it doesn't have comment_author_url nor comment_author_email. It has comment_post_ID, comment_content, comment_parent, comment_type, user_id, comment_author. – theyuv Commented Oct 14, 2016 at 17:37
Add a comment  | 

1 Answer 1

Reset to default 1

The example from the codex, the array of argument is now required.

global $post, $current_user; //for this example only :)

    $commentdata = array('comment_post_ID' => $post->ID, // to which post the comment will show up
        'comment_author' => 'Another Someone', //fixed value - can be dynamic 
        'comment_author_email' => '[email protected]', //fixed value - can be dynamic 
        'comment_author_url' => 'http://example', //fixed value - can be dynamic 
        'comment_content' => 'Comment messsage...', //fixed value - can be dynamic 
        'comment_type' => '', //empty for regular comments, 'pingback' for pingbacks, 'trackback' for trackbacks
        'comment_parent' => 0, //0 if it's not a reply to another comment; if it's a reply, mention the parent comment ID here
        'user_id' => $current_user->ID, //passing current user ID or any predefined as per the demand
   );

  //Insert new comment and get the comment ID
  $comment_id = wp_new_comment( $commentdata );

You can have a look to wp_filter_comment(), the source shows some filters that set fill $commentdata. Maybe one of these are used by the plugin you use.

发布评论

评论列表(0)

  1. 暂无评论