Last active
December 10, 2019 04:54
-
-
Save sardisson/e83e52b8098c0d80894cc7bb2fd1d718 to your computer and use it in GitHub Desktop.
WordPress filter to auto-linkify @NAMEs for Micro.blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Auto-linkify @names for Micro.blog */ | |
/* Add to your child theme's functions.php or your site's functionality plugin */ | |
/* You should not put this in a stock theme's functions.php because any update */ | |
/* to that theme will overwrite functions.php and this code will have to be */ | |
/* added back again. */ | |
// Props to Chris Reed for his helpful pointers to make this only run on actual posts | |
function linkify_microdotblog_names( $data ) { | |
$content = $data['post_content']; | |
$post_type = $data['post_type']; | |
if ( $post_type == 'post' ) { | |
$string = $content; | |
$pattern = '/(^|\s)@([A-Za-z0-9_]+)/'; | |
$replacement = '\1<a href="https://micro.blog/\2">@\2</a>'; | |
$content = preg_replace( $pattern, $replacement, $string ); | |
$data['post_content'] = $content; | |
} | |
return $data; | |
} | |
add_filter( 'wp_insert_post_data', 'linkify_microdotblog_names' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@vishae Sorry for the late response; I didn’t get any notification of a new comment and just happened to drop in here :-(
It’s absolutely possible and I think should be easy; let me play around with it before I post something. Can you @ me here (operating under the assumptions that @-ing someone on gist generates a notification like it does on the main site) or on Micro.blog if I haven’t put something up in a week?