首先,您必须为 wordpress 构建一个插件,该插件可以与已发布的帖子或更新帖子挂钩,您可以参考this
之后,您可以在他们在 post_content 上找到 hastag 时添加标签,代码如下所示
function post_published_notification( $ID, $post ) {
$content = $post->post_content;
preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
if(isset($matches[1])){
foreach($matches[1] as $matchKey){
wp_set_post_tags( $ID, trim($matchKey), true);
}
}
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );
如果你使用前沿职位也许你可以使用这个
function post_published_from_frontier($my_post){
$content = $my_post->post_content;
$ID = $my_post->ID;
preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
if(isset($matches[1])){
foreach($matches[1] as $matchKey){
wp_set_post_tags( $ID, trim($matchKey), true);
}
}
}
add_action('frontier_post_post_save', post_published_from_frontier, 10 ,2 );
add_action优先级参数可以参考this
并将帖子中的所有hastag更改为url,您可以使用这样的代码
function old_wp_content( $content ) {
$content = preg_replace('/ #([A-Za-z0-9\/\.]*)/', ' <a target=\"_blank\" href=\"https://milyin.com/hashtag/$1\">$1</a>', $content);
return $content;
}
add_filter( 'the_content', 'old_wp_content' );
所以如果我们将所有代码合并到一个插件中,我们可以像这样使用它
<?php
function post_published_notification( $ID, $post ) {
$content = $post->post_content;
preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
if(isset($matches[1])){
foreach($matches[1] as $matchKey){
wp_set_post_tags( $ID, trim($matchKey), true);
}
}
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );
function post_published_from_frontier($my_post){
$content = $my_post->post_content;
$ID = $my_post->ID;
preg_match_all('/( #\w+)/', $content, $matches, PREG_PATTERN_ORDER);
if(isset($matches[1])){
foreach($matches[1] as $matchKey){
wp_set_post_tags( $ID, trim($matchKey), true);
}
}
}
add_action('frontier_post_post_save', post_published_from_frontier, 10 ,2 );
function old_wp_content( $content ) {
$content = preg_replace('/ #([A-Za-z0-9\/\.]*)/', ' <a target=\"_blank\" href=\"https://milyin.com/hashtag/$1\">$1</a>', $content);
return $content;
}
add_filter( 'the_content', 'old_wp_content' );