wp_insert_comment()添加一个新的评论到数据库中
wp_insert_comment()添加一个新的评论到数据库中
目录
描述
译文
在数据库中插入评论。
有效的$commetdata关键字名称包括 ‘comment_author_IP’, ‘comment_date’, ‘comment_date_gmt’, ‘comment_parent’, ‘comment_approved’, 及’user_id’。
原文
Inserts a comment to the database.
The available $commentdata key names are ‘comment_author_IP‘, ‘comment_date‘, ‘comment_date_gmt‘, ‘comment_parent‘, ‘comment_approved‘, and ‘user_id‘.
Also, consider using wp_new_comment(), which sanitizes and validates comment data before calling wp_insert_comment() to insert the comment into the database.
用法
1
2
3
4
|
/* ———————————-
* wordpress之魂 © http://wphun.com
* ———————————- */
<?php$time = current_time(‘mysql’);$data = array( ‘comment_post_ID’ => 1, ‘comment_author’ => ‘admin’, ‘comment_author_email’ => ‘admin@admin.com’, ‘comment_author_url’ => ‘http://’, ‘comment_content’ => ‘content here’, ‘comment_type’ => ”, ‘comment_parent’ => 0, ‘user_id’ => 1, ‘comment_author_IP’ => ‘127.0.0.1’, ‘comment_agent’ => ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)’, ‘comment_date’ => $time, ‘comment_approved’ => 1,);wp_insert_comment($data);?>
|
参数
返回值
注意
- 使用到: $wpdb
- 使用到: current_time()
- 使用到: get_gmt_from_date()
- 使用到: wp_update_comment_count()
- 使用到:Inserts a record in the comments table in the database
历史
添加于 版本: 2.0.0
源文件
wp_insert_comment() 函数的代码位于 wp-includes/comment.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/* ———————————-
* wordpress之魂 © http://wphun.com
* ———————————- */
/**
* Inserts a comment into the database.
*
* @since 2.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $commentdata {
* Array of arguments for inserting a new comment.
*
* @type string $comment_agent The HTTP user agent of the `$comment_author` when
* the comment was submitted. Default empty.
* @type int|string $comment_approved Whether the comment has been approved. Default 1.
* @type string $comment_author The name of the author of the comment. Default empty.
* @type string $comment_author_email The email address of the `$comment_author`. Default empty.
* @type string $comment_author_IP The IP address of the `$comment_author`. Default empty.
* @type string $comment_author_url The URL address of the `$comment_author`. Default empty.
* @type string $comment_content The content of the comment. Default empty.
* @type string $comment_date The date the comment was submitted. To set the date
* manually, `$comment_date_gmt` must also be specified.
* Default is the current time.
* @type string $comment_date_gmt The date the comment was submitted in the GMT timezone.
* Default is `$comment_date` in the site’s GMT timezone.
* @type int $comment_karma The karma of the comment. Default 0.
* @type int $comment_parent ID of this comment’s parent, if any. Default 0.
* @type int $comment_post_ID ID of the post that relates to the comment, if any.
* Default empty.
* @type string $comment_type Comment type. Default empty.
* @type int $user_id ID of the user who submitted the comment. Default 0.
* }
* @return int|false The new comment’s ID on success, false on failure.
*/
function wp_insert_comment( $commentdata ) {
global $wpdb;
$data = wp_unslash( $commentdata );
$comment_author = ! isset( $data[‘comment_author’] ) ? ” : $data[‘comment_author’];
$comment_author_email = ! isset( $data[‘comment_author_email’] ) ? ” : $data[‘comment_author_email’];
$comment_author_url = ! isset( $data[‘comment_author_url’] ) ? ” : $data[‘comment_author_url’];
$comment_author_IP = ! isset( $data[‘comment_author_IP’] ) ? ” : $data[‘comment_author_IP’];
$comment_date = ! isset( $data[‘comment_date’] ) ? current_time( ‘mysql’ ) : $data[‘comment_date’];
$comment_date_gmt = ! isset( $data[‘comment_date_gmt’] ) ? get_gmt_from_date( $comment_date ) : $data[‘comment_date_gmt’];
$comment_post_ID = ! isset( $data[‘comment_post_ID’] ) ? ” : $data[‘comment_post_ID’];
$comment_content = ! isset( $data[‘comment_content’] ) ? ” : $data[‘comment_content’];
$comment_karma = ! isset( $data[‘comment_karma’] ) ? 0 : $data[‘comment_karma’];
$comment_approved = ! isset( $data[‘comment_approved’] ) ? 1 : $data[‘comment_approved’];
$comment_agent = ! isset( $data[‘comment_agent’] ) ? ” : $data[‘comment_agent’];
$comment_type = ! isset( $data[‘comment_type’] ) ? ” : $data[‘comment_type’];
$comment_parent = ! isset( $data[‘comment_parent’] ) ? 0 : $data[‘comment_parent’];
$user_id = ! isset( $data[‘user_id’] ) ? 0 : $data[‘user_id’];
$compacted = compact( ‘comment_post_ID’, ‘comment_author’, ‘comment_author_email’, ‘comment_author_url’, ‘comment_author_IP’, ‘comment_date’, ‘comment_date_gmt’, ‘comment_content’, ‘comment_karma’, ‘comment_approved’, ‘comment_agent’, ‘comment_type’, ‘comment_parent’, ‘user_id’ );
if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
return false;
}
$id = (int) $wpdb->insert_id;
if ( $comment_approved == 1 ) {
wp_update_comment_count( $comment_post_ID );
}
$comment = get_comment( $id );
/**
* Fires immediately after a comment is inserted into the database.
*
* @since 2.8.0
*
* @param int $id The comment ID.
* @param obj $comment Comment object.
*/
do_action( ‘wp_insert_comment’, $id, $comment );
wp_cache_set( ‘last_changed’, microtime(), ‘comment’ );
return $id;
}
|
- 原文:http://codex.wordpress.org/Function_Reference/wp_insert_comment
- 翻译:黄聪@WordPress之魂
Leave a Reply
要发表评论,您必须先登录。