WordPress, Micropub and Featured Images

I haven’t yet figured out if and how I can set Featured Images through Indigenous and Micropub (I can’t really find a whole lot about them in the Micropub spec, either, but that could be me), but I sure have image uploads working!

Now, because I typically only show Featured Images, and almost never upload more than one image, and have pretty much disabled WordPress’s [gallery] shortcode, I thought it’d be nice to just make the first uploaded image the Featured Image.

The Micropub plugin does all of the heavy lifting here, and it already correctly attaches freshly uploaded images to newly created posts. It even stores a list of images as post metadata. (I’ve yet to figure out how to act on that, though. There’s no easy-to-use insert_post_meta action hook. I think.)

The good thing is, I can attach something like the following callback to after_micropub just fine. (Hurray!)

add_action( 'after_micropub', function( $input, $wp_args ) {
  if ( empty( $wp_args['ID'] ) || empty( $input['properties']['photo'][0] ) ) {
    return;
  }

  $photo_id = attachment_url_to_postid( $input['properties']['photo'][0] );

  if ( empty( $photo_id ) ) {
    return;
  }

  set_post_thumbnail( $wp_args['ID'], $photo_id );
}, 10, 2 );

It’s all a bit backward, converting an attachment URL into an attachment ID, and then setting that attachment as its parent’s Featured Image, but it works. (Note to self: check if $wp_args maybe contains the post ID, which would allow for a bit less code. A previous version used a slightly different approach.)

The one thing I was worried about is that all of this runs after the post has already been created and that the Featured Image would not get included in syndicated copies. Luckily, Share on Mastodon happily supports mere attached images as well, so that solves that. (The same isn’t true for Share on Pixelfed, yet, so I’m going to have to address that.) Good news: micropub_syndication runs after after_micropub!

Except … I mean, why does that even work? The Share plugins hook into transition_post_status and that gets called during wp_insert_post() already, which is before Micropub attaches the uploaded images.

A: 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 “re-trigger” until after this bit of code.

Of note: I’ve had people report Mastodon copies with missing Featured Images, for posts created in WordPress’s official app, although I couldn’t quite reproduce the issue. (Can’t say I really use the app, though.) But if this really is an issue and there’s no hook we can use to re-trigger transition_post_status after images are attached, scheduling crossposting into the (near) future might be an option.

Speaking of syndication, you might think, “Why not just splice the set_post_thumbnail() bit into the micropub_syndication callback?” And you would not be wrong. Except, that gets called only when one of the syndication targets was actually checked.

One response to “WordPress, Micropub and Featured Images”

  1. Johan Avatar

    … favorited this!