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.
Frank Meeuwsen on
Twee vliegen in één klap slaan op deze zondagochtend. Nu eerst de brok code testen die Jan me zojuist via Mastodon stuurt. En zien of deze post ook op Mastodon verschijnt met zijn eigen “Send to Mastodon” plugin.
Via diggingthedigital.com, in reply to .
Jan Boddez on
[…] previously talked about how I crosspost from this blog to my Mastodon account without the need for a third-party service, and how I leverage WordPress’s hook system to even enable toot […]
Via jan.boddez.net, in reply to .
[…] It works, well, because I explicitly re-trigger transition_post_status when I tackle syndication. (Like, syndication isn’t even “on” when Micropub posts get first created.) Nice. I can even make it work for Share on Pixelfed, I believe, by delaying that previously mentioned […]
Via jan.boddez.net, in reply to .
[…] could even use that same hook to automatically syndicate posts based on their categories or whatever. (Heck, you could go all the way and implement Micropub syndication targets, using hooks provided by the Micropub plugin instead. Everything’s possible, but try explaining […]
Via jan.boddez.net, in reply to .