Skip to content

Instantly share code, notes, and snippets.

@sardisson
Last active December 10, 2019 04:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sardisson/e83e52b8098c0d80894cc7bb2fd1d718 to your computer and use it in GitHub Desktop.
Save sardisson/e83e52b8098c0d80894cc7bb2fd1d718 to your computer and use it in GitHub Desktop.
WordPress filter to auto-linkify @NAMEs for Micro.blog
/* 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' );
@sardisson
Copy link
Author

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment