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

sardisson commented Jan 24, 2018

You can also use almost the same code to do this in WordPress comments; you just need a second function with a slightly different name, acting on $comment instead of $content, and your add_filter() hooks comment_save_pre and calls the second function:

function linkify_microdotblog_names_comments( $comment ) {
  $string = $comment;
  $pattern = '/(^|\s)@([A-Za-z0-9_]+)/';
  $replacement = '\1<a href="https://micro.blog/\2">@\2</a>';

  $content = preg_replace($pattern, $replacement, $string);
  return $comment;
}

add_filter( 'comment_save_pre', 'linkify_microdotblog_names_comments', 10, 1 );

Note, though, that any @NAMEs in native WordPress comments (i.e., comments originating on your blog) won’t show up (or be Mentions) on Micro.blog unless you 1) send Webmentions back to Micro.blog for the comment and 2) the comment author also has a Micro.blog account that can be matched from the comment information. So this additional function is more of a neat trick than a useful bit of code.

@vishae
Copy link

vishae commented Aug 15, 2018

I see that this function searches for the pattern "@user" to replace it with an mb link.

I'm not great with php or regex, but would it be possible ot have it search for m@user for mb user link and t@user for twitter user link?

@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