This snippet allows you to convert a WordPress tag into a hashtag that links to a tag.
It works by scanning the content for the octothorp symbol (#) and changing the word right after the # symbol to a tag-link.
Instructions:
Add this snippet to the functions.php file of your active theme.
function wptag_to_hashtag($mywp_post)
{
$content = $mywp_post->post_content;
$ID = $mywp_post->ID;
preg_match_all('/\B(\#[a-zA-Z]+\b)/', $content, $matches, PREG_PATTERN_ORDER);
if (isset($matches[1])) {
foreach ($matches[1] as $matchKey) {
wp_set_post_tags($ID, $matchKey, true);
}
}
}
add_action('post_save', 'wptag_to_hashtag', 10, 2);
function hashtagger($content)
{
$siteurl = get_site_url(); // get wordpress siteurl to create url for tags
$content = preg_replace('/([^a-zA-Z-_&])#([a-zA-Z_]+)/', "$1<a class=\"hashtags\" href=\"$siteurl/tag/$2\" target=\"_parent\" >#$2</a>", $content);
return $content;
}
add_filter('the_content', 'hashtagger');
Comments