Skip to content

Customize “Share on Mastodon” Statuses

Sometime last year, I wrote a (rather opinionated) plugin to help bloggers share WordPress posts on Mastodon—an open-source and decentralized microblogging service. It’s super simple on purpose. While the Settings page lets you choose which posts will be POSSE’d—“Publish on your Own Site, Syndicate Elsewhere”—most “customizing” is made possible through filter hooks.

By default, shared statuses look something like:

My Awesome Post Title https://url.to/original-post/

Mastodon is smart enough to then try and find things like an Open Graph image and description for that URL. (There’s no need for a link shortener, either.)

If you wanted to automatically append WordPress tags as hashtags, however, you could easily do that:

add_filter( 'share_on_mastodon_status', function( $status, $post ) {
  $tags = get_the_tags( $post->ID );

  if ( $tags ) {
    $status .= "\n\n";
    
    foreach ( $tags as $tag ) {
      $status .= '#' . preg_replace( '/\s/', '', $tag->name ) . ' ';
    }

    $status = trim( $status );
  }

  return $status;
}, 10, 2 );

Note: It may—depending on your specific situation—be wise to somehow escape $tag->name, as I’m not exactly sure what special characters might be present, or how Mastodon would treat those. (All I’m doing here, obviously, is remove whitespace. [I should probably convert “multi-word” hashtags to CamelCase, too, which helps screen readers read them out loud correctly.])

It’s possible to share more than just the title, too. On my blog, where I only share (very short) notes, I more or less do the following:

add_filter( 'share_on_mastodon_status', function( $status, $post ) {
  $status  = wp_strip_all_tags( $post->post_content );
  $status .= "\n\n" . get_permalink( $post );
  return $status;
}, 10, 2 );

Note: While Mastodon accepts some HTML, I tend to filter it all out. In fact, by calling (and sending) $post->post_content, without any filters applied, my Mastodon followers will see a very plain-text version of these brief status updates, followed by a backlink.

Replies

  1. Jan Boddez on

    […] see if I can get “Webmention comments” on my earlier post on how to modify my Mastodon crossposter’s behavior to include a bit of context, too, like the pingbacks of […]

    Via jan.boddez.net, in reply to Customize “Share on Mastodon” Statuses.

  2. Jan Boddez on

    […] tags, so it should be safe to reuse. (In fact, it may need to be modified a bit, to explicitly keep some HTML, just like “normal” WordPress comments.) And, because selfdogfood, I’m using it on this site, […]

    Via jan.boddez.net, in reply to Customize “Share on Mastodon” Statuses.