Skip to content

Micropub, Syndication Targets, and Crossposting to Mastodon

I use Indigenous for Android to create the vast majority of notes on this site, rather than WordPress’s “official” app—which last I checked doesn’t play nice with Custom Post Types. Indigenous “speaks” Micropub, a kind of CMS-agnostic protocol for creating and editing all types of content, which WordPress supports by means of a wonderful little plugin.

One of the many things Micropub offers, is a way to indicate which other platforms posts should be syndicated—or POSSE’d, or crossposted—to. (It’s up to WordPress, in our case, to then honor this request and act accordingly.)

So. I also use a very simple plugin to share my notes (and articles) on my Mastodon account, which lets me choose, on a per-post basis, if I want or don’t want something crossposted, and uses a custom meta box to do so.

Such a meta box, of course, exists only inside WordPress’s admin interface. And thus, when I post through Micropub, that meta value (or custom field) does not automatically get set. Luckily, the Micropub plugin comes with a couple action hooks that we can attach callbacks to in order to work around this limitation. I mean, Micropub, as already mentioned, was literally designed for this type of use case.

The first thing I’m taking care of is telling my Micropub client what external platforms (or syndication targets) I’m looking to support. (I’m adding this code to a site-specific plugin, although my child theme’s functions.php would work equally well.)

add_filter( 'micropub_syndicate-to', function( $syndicate_to, $user_id ) {
  return array(
    array(
      'uid'  => 'https://geekcompass.com/@jan',
      'name' => 'Mastodon',
    ),
  );
}, 10, 2 );

Next up is the actual logic that gets run when a new Micropub request contains a syndication target.

add_action( 'micropub_syndication', function( $post_id, $synd_requested ) {
  if ( in_array( 'https://geekcompass.com/@jan', $synd_requested, true ) ) {
    // Post should be syndicated to Mastodon.
    update_post_meta( $post_id, '_share_on_mastodon', '1' ); // The meta field used by Share on Mastodon.

    $post = get_post( $post_id );

    if ( 'publish' === $post->post_status ) {
      // Re-trigger Share on Mastodon's `Post_Handler::toot()` method.
      wp_transition_post_status( 'publish', 'publish', $post );
    }
  }
}, 10, 2 );

Note: I’m very well aware that last statement may trigger quite a few more actions, possibly unintentionally. At least for me, there’s no problem here—I’m quite aware of how my site behaves under the hood. In fact, I use a somewhat different version of this code to trigger POSSE’ing to multiple syndication targets all at the same time.

Obviously, the code snippets above won’t do much if the specific combination of Micropub and Share on Mastodon isn’t active. They’re just one example of how one can tap into the Micropub plugin’s different syndication-related hooks.