Cross-posting from WordPress to Mastodon

I’ve finally got WordPress to Mastodon cross-posting working the way I want: automatically, whether I’m posting through the WordPress web interface or through a desktop or mobile client like MarsEdit or the WordPress mobile app, and with the format that I want:

Title: Excerpt (#tags)

Full post on Eclecticism: URL

I’d been using the Autopost to Mastodon plugin, which works great, and I can recommend it — as long as you only or primarily post using the WordPress web interface.

However, the plug-in is only triggered when publishing a post through the WordPress web interface. Any time I posted through a client, nothing went to Mastodon. So I either had to go into the web interface and manually trigger an update to the post with the “Send to Mastodon” option checked, or just skip out on using anything but the web interface at all, which I’m not a fan of (especially on mobile).

I’d asked the plug-in author, and they’ve said that this is just the way it is.

So I put out a call for help on Mastodon, and got some kind tips from Elephantidae, who pointed out the Share on Mastodon plugin. This looked promising, as its documentation specifically mentions being able to configure it to work with externally created posts. However, looking through the docs made it clear that most of this plugin’s configuration, including changing the format of the text it sends to Mastodon, is done through adding and tweaking PHP functions…and as with most of my coding knowledge, my PHP knowledge is roughly at the “I can usually get a vauge idea of what it’s doing when I read the code, but actually creating something is a whole different ballgame” territory. Plus, dumping PHP code into my theme’s files risks losing those changes the next time the theme files are updated.

Retaining the code through theme updates can be managed through creating a site-specific plugin, however — a handy trick which, somewhat amusingly, I’d never had exactly the right combination of “I want to do this” and “how do I do it” in the past to discover until now.

So, after a bit of fumbling around with the Share on Mastodon plugin documentation and figuring out the right PHP and WordPress function calls, here’s what I’ve ended up adding to my site-specific plugin:

/* Tweaks for the Share on Mastodon plugin */

/* Customize sharing text */

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

  $status = get_the_title( $post ) . ": " . get_the_excerpt( $post );

  if ( $tags ) {
    $status .= " (";

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

    $status = trim( $status );  
    $status .= ")";
    }

  $status .= "\n\nFull post on Eclecticism: " . get_permalink( $post );
  return html_entity_decode( $status );
  return $status;
}, 10, 2 );

/* Share if sent through XML-RPC */

add_filter ('share_on_mastodon_enabled', '__return_true');

/* End Share on Mastodon tweaks */

And after a few tests to fine-tune everything, it all seems to work just the way I wanted. Success!

(Also, re-reading through this, I’ve realized that since I like to give the background of why and how I stumble my way through things, I end up writing posts that are basically a slightly geekier version of the “stop telling me about your childhood vacations to Europe and just post the damn recipe!” posts that are commonly mocked. And I don’t even have ad blocks all over my site! At least I’m not making you click through several slideshow pages of inane chatter before I get to the good stuff. My inane chatter is easy to scroll through.)