将分类法标签添加到 wordpress post,方法是将其写在带有主题标签的评论中
Adding a taxonomy tag to wordpress post by writing it in comment with hashtag
在 wordpress 中,我有一个带有一些标签的 post。用户应该能够通过在评论中使用主题标签编写标签来向 post 添加标签,例如'This is a comment that adds #orange' 应添加橙色标签。
这是我的代码:
function add_tag_from_comment( $comment_ID ) {
$comment = get_comment($comment_ID);
$search_text = strip_tags( str_replace( array( "\n", "\r"), $comment->comment_content));
preg_match_all('/#([\d\w-_]+)[\b|]{0,3}/', $search_text, $matches, PREG_PATTERN_ORDER);
foreach($matches[1] as $match) {
wp_set_post_tags( $comment->comment_post_ID, $match, true );
}
}
add_action( 'comment_post', 'add_tag_from_comment', 10, 2 );
如果我用 'This is a comment that adds #oranges' 之类的文本替换 $comment->comment_content
,那么它就可以工作。但是当我写实际评论时它不起作用而且我不知道原因。有人可以帮助我吗?
替换
$comment = get_comment($comment_ID);
和
$comment = get_comments($comment_ID);
add_action('comment_post', 'tag_comment_insert', 2);
function tag_comment_insert($comment) {
$comment_text = get_comment_text($comment);
preg_match_all('/#([0-9a-zA-Z]+)/', $comment_text, $matches, PREG_PATTERN_ORDER);
wp_set_post_tags( $comment, $matches[1], true );
}
add_action('comment_text', 'tag_comment', 2);
function tag_comment($comment) {
$comment = preg_replace('/#([0-9a-zA-Z]+)/i', '<a class="hashtag" href="'.get_home_url().'/tag/">#</a>', $comment);
return $comment;
}
我找到了基于 的解决方案:
add_action('comment_post', 'tag_comment_insert', 2);
function tag_comment_insert($comment) {
$comment_text = get_comment_text($comment);
$current_comment = get_comment( $comment );
$comment_post_id = $current_comment->comment_post_ID;
preg_match_all('/#([\d\w-_]+)[\b|]{0,3}/', $comment_text, $matches, PREG_PATTERN_ORDER);
wp_set_post_tags( $comment_post_id, $matches[1], true );
}
add_action('comment_text', 'tag_comment', 2);
function tag_comment($comment) {
$comment = preg_replace('/#([0-9a-zA-Z]+)/i', '<a class="hashtag" href="'.get_home_url().'/tag/">#</a>', $comment);
return $comment;
}
之前的问题是post_ID
没有设置。我的解决方案似乎有点复杂,因此欢迎任何缩短。谢谢大家的帮助。
在 wordpress 中,我有一个带有一些标签的 post。用户应该能够通过在评论中使用主题标签编写标签来向 post 添加标签,例如'This is a comment that adds #orange' 应添加橙色标签。
这是我的代码:
function add_tag_from_comment( $comment_ID ) {
$comment = get_comment($comment_ID);
$search_text = strip_tags( str_replace( array( "\n", "\r"), $comment->comment_content));
preg_match_all('/#([\d\w-_]+)[\b|]{0,3}/', $search_text, $matches, PREG_PATTERN_ORDER);
foreach($matches[1] as $match) {
wp_set_post_tags( $comment->comment_post_ID, $match, true );
}
}
add_action( 'comment_post', 'add_tag_from_comment', 10, 2 );
如果我用 'This is a comment that adds #oranges' 之类的文本替换 $comment->comment_content
,那么它就可以工作。但是当我写实际评论时它不起作用而且我不知道原因。有人可以帮助我吗?
替换
$comment = get_comment($comment_ID);
和
$comment = get_comments($comment_ID);
add_action('comment_post', 'tag_comment_insert', 2);
function tag_comment_insert($comment) {
$comment_text = get_comment_text($comment);
preg_match_all('/#([0-9a-zA-Z]+)/', $comment_text, $matches, PREG_PATTERN_ORDER);
wp_set_post_tags( $comment, $matches[1], true );
}
add_action('comment_text', 'tag_comment', 2);
function tag_comment($comment) {
$comment = preg_replace('/#([0-9a-zA-Z]+)/i', '<a class="hashtag" href="'.get_home_url().'/tag/">#</a>', $comment);
return $comment;
}
我找到了基于
add_action('comment_post', 'tag_comment_insert', 2);
function tag_comment_insert($comment) {
$comment_text = get_comment_text($comment);
$current_comment = get_comment( $comment );
$comment_post_id = $current_comment->comment_post_ID;
preg_match_all('/#([\d\w-_]+)[\b|]{0,3}/', $comment_text, $matches, PREG_PATTERN_ORDER);
wp_set_post_tags( $comment_post_id, $matches[1], true );
}
add_action('comment_text', 'tag_comment', 2);
function tag_comment($comment) {
$comment = preg_replace('/#([0-9a-zA-Z]+)/i', '<a class="hashtag" href="'.get_home_url().'/tag/">#</a>', $comment);
return $comment;
}
之前的问题是post_ID
没有设置。我的解决方案似乎有点复杂,因此欢迎任何缩短。谢谢大家的帮助。