Php 如何在WordPress评论中添加带有链接的hashtags和cashtags?
好的,通过在functions.php中添加这一行,我成功地使它适用于_内容:Php 如何在WordPress评论中添加带有链接的hashtags和cashtags?,php,regex,wordpress,Php,Regex,Wordpress,好的,通过在functions.php中添加这一行,我成功地使它适用于_内容: add_filter('the_content', 'my_hashcash_class'); function my_hashcash_class($content){ $content = preg_replace('/(\$|#)(\w+)[^\w".;]/s', '<a href="https://www.mywebsite.com/?s=\2">\1\
add_filter('the_content', 'my_hashcash_class');
function my_hashcash_class($content){
$content = preg_replace('/(\$|#)(\w+)[^\w".;]/s', '<a href="https://www.mywebsite.com/?s=\2">\1\2</a> ', $content);
return $content;
}
评论甚至不显示
以下是我的评论代码:
add_filter('the_comments', 'my_hashcash_comments_class');
function my_hashcash_comments_class($comments){
$comments = preg_replace('/(\$|#)(\w+)[^\w".;]/s', '<a href="https://www.mywebsite.com/?s=\2">\1\2</a> ', $comments);
return $comments;
}
add_filter('the_comments'、'my_hashcash_comments_class');
函数my_hashcash_comments_类($comments){
$comments=preg\u replace('/(\$\124;#))(\ w+[^\w.;]/s','','',$comments);
返回$comments;
}
也许WordPress正在尝试对评论人的用户名、头像和所有其他东西应用过滤器
我希望过滤器只应用于评论内容本身
任何帮助都将不胜感激
谢谢。请尝试下面的代码。“$comments”参数是WP\u Comment对象的数组对象,因此您可以通过循环进行访问
add_filter('the_comments', 'my_hashcash_comments_class');
function my_hashcash_comments_class($comments){
foreach ( $comments as $key => $comment ) {
$comments[$key]->comment_content = preg_replace( '/(\$|#)(\w+)[^\w".;]/s', '<a href="https://www.mywebsite.com/?s=\2">\1\2</a>', $comment->comment_content );
}
return $comments;
}
add_filter('the_comments'、'my_hashcash_comments_class');
函数my_hashcash_comments_类($comments){
foreach($key=>$comment作为注释){
$comments[$key]->comment\u content=preg\u replace('/(\$\124;#))(\ w+[^\w.;]/s',''.$comment->comment\u content);
}
返回$comments;
}
尝试下面的代码。“$comments”参数是WP_Comment对象的数组对象,因此您可以通过循环进行访问
add_filter('the_comments', 'my_hashcash_comments_class');
function my_hashcash_comments_class($comments){
foreach ( $comments as $key => $comment ) {
$comments[$key]->comment_content = preg_replace( '/(\$|#)(\w+)[^\w".;]/s', '<a href="https://www.mywebsite.com/?s=\2">\1\2</a>', $comment->comment_content );
}
return $comments;
}
add_filter('the_comments'、'my_hashcash_comments_class');
函数my_hashcash_comments_类($comments){
foreach($key=>$comment作为注释){
$comments[$key]->comment\u content=preg\u replace('/(\$\124;#))(\ w+[^\w.;]/s',''.$comment->comment\u content);
}
返回$comments;
}
我没有WP,但从我的Bhautik的回答来看,您处理的是一个对象数组。因为对象在循环中保持可变,所以您可以直接将更改应用于对象,而无需通过其键引用它
您的正则表达式模式可能需要一些改进。当匹配文本$
和#
时,只需将它们放在字符类中,避免管道和转义
\w+
是合适的,但不需要以下否定字符类。事实上,如果字符串中的最后一个字符仍然是哈希/现金子字符串的\w
,则会导致模式失败
s
模式修饰符只有在模式中有非文字的
时才有任何值。您没有,因此模式修饰符没有任何用处——请忽略它
代码:()
$comments=[
(对象)['comment_content'=>“Hello$there fella'],
(对象)['comment_content'=>'Hashy#tag123 text']
];
foreach($comments作为$comment){
$comment->comment\u content=preg\u replace(
“~[$#](\w+)”,
'',
$comment->comment\u内容
);
}
var_导出(注释);
输出:
array (
0 =>
(object) array(
'comment_content' => 'Hello <a href="https://www.example.com/?s=there">$there</a> fella',
),
1 =>
(object) array(
'comment_content' => 'Hashy <a href="https://www.example.com/?s=tag123">#tag123</a> text',
),
)
数组(
0 =>
(对象)数组(
“评论内容”=>“你好,小伙子”,
),
1 =>
(对象)数组(
'注释内容'=>'哈希文本',
),
)
我没有WP,但从我的Bhautik的回答来看,您处理的是一个对象数组。因为对象在循环中保持可变,所以您可以直接将更改应用于对象,而无需通过其键引用它
您的正则表达式模式可能需要一些改进。当匹配文本$
和#
时,只需将它们放在字符类中,避免管道和转义
\w+
是合适的,但不需要以下否定字符类。事实上,如果字符串中的最后一个字符仍然是哈希/现金子字符串的\w
,则会导致模式失败
s
模式修饰符只有在模式中有非文字的
时才有任何值。您没有,因此模式修饰符没有任何用处——请忽略它
代码:()
$comments=[
(对象)['comment_content'=>“Hello$there fella'],
(对象)['comment_content'=>'Hashy#tag123 text']
];
foreach($comments作为$comment){
$comment->comment\u content=preg\u replace(
“~[$#](\w+)”,
'',
$comment->comment\u内容
);
}
var_导出(注释);
输出:
array (
0 =>
(object) array(
'comment_content' => 'Hello <a href="https://www.example.com/?s=there">$there</a> fella',
),
1 =>
(object) array(
'comment_content' => 'Hashy <a href="https://www.example.com/?s=tag123">#tag123</a> text',
),
)
数组(
0 =>
(对象)数组(
“评论内容”=>“你好,小伙子”,
),
1 =>
(对象)数组(
'注释内容'=>'哈希文本',
),
)
请共享该注释代码。使用注释代码更新。看到复数单词$comments
我想知道它是否是数组而不是字符串?请共享该注释代码。使用注释代码更新。看到复数单词$comments
我想知道它是否是数组而不是字符串一串