Testing some fediversal URL auto embeds for WordPress happening in this here blog, now with slapping a naked URL on its own line:
https://cosocial.ca/@cogdog/115613213272065972
which embeds beautifully just like this- SHAZAM!~
Can we go beyond mastodon? What about a pixelfed URL?
https://pixelfed.social/p/cogdog/873949556324687103
and yup. I am seeing an embedded pixelfed photo here. WINNING!
As it turns out I have been wrong about how this is working. I had been using some code I have stuffed in a Cogdog Auto Embed Extras WordPress Plugin I have for a variety of extra auto embeds, used elsewhere in SPLOTbox theme. Typically these are set up because the URL patterns are pretty easy to match, but with fedistuff, the domains are not known in advance,
The relevant code for Mastodon and Pixelfeed is a regex search that looks for URLs like:
https://any.instance.blah/@[username_string]/string_of_digits0-9/
It seems to match all fediverse URLs, I guess it could barf on something else. For the maybe one reader who might follow along,these are set up be registering the embed patterns.
add_action( 'init', 'cdb_register_embeds' );
function cdb_register_embeds() {
// handler for mastodon
wp_embed_register_handler(
'mastodon',
'#^https?:\/\/((?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9])\/@([a-zA-Z0-9\_]+)\/(\d+)$#i',
'cdb_handler_mastodon'
);
// handler for pixelfed
wp_embed_register_handler(
'pixelfed',
'#^https?:\/\/((?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9])\/p\/[a-zA-Z0-9\_]+\/(\d+)$#i',
'cdb_handler_pixelfed'
);
}
And these each reference a function called to embed them as iframes:
function cdb_handler_mastodon( $matches, $attr, $url, $rawattr ) {
// handler for mastodon embeds
$embed = '<iframe src="https://' . esc_attr($matches[1]) . '/@' . esc_attr($matches[2]) . '/' . esc_attr($matches[3]) . '/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="800" allowfullscreen="allowfullscreen"></iframe><script src="https://' . esc_attr($matches[1]) . '/embed.js" async="async"></script>';
return $embed;
}
function cdb_handler_pixelfed( $matches, $attr, $url, $rawattr ) {
// handler for pixelfed embeds
$embed = '<iframe src="' . esc_attr($matches[0]) . '/embed?caption=false&likes=false&layout=compact" class="pixelfed__embed" style="max-width:100%; min-height:600px; border:0" width="600" allowfullscreen="allowfullscreen"></iframe><script async defer src="https:/' . esc_attr($matches[1]) . '/embed.js" async="async"></script>';
return $embed;
}
But then, while testing the ActivityPub setup I had toyed with on WordPress.com when it first came out. In writing that post, I could see WordPress was autoembedding a Mastodon URL — how was it doing that?
First I commented out my crufty duct tape code above, and redid the two embeds at the top of the post- the pixelfed one is much better than what I had before.
So I started testing on a few other websites and discovered:
- I do not get fediverse autoembeds on many sites (It renders the naked URL)
- It does work only on sites with the ActivtyPub plugin enabled, I get a embed,
What to do? I download the ActivityPub plugin, poke around the include directory, and find a file class-embed.php which indicates at the top:
<?php
/**
* ActivityPub Embed Handler.
*
* @package Activitypub
*/
namespace Activitypub;
/**
* Class to handle embedding ActivityPub content.
*/
class Embed {
:
:
I am not sure I understand everything, but I am sure that enabling the ActivtyPub plugin on a WordPress sites gives as a bonus the autoembeds of fediverse URLs. I can’t find this mentioned anywhere I have looked so far.
With this I live up to the spirit of my pinned Mastodon post, which I can embed with just pasting its URL in here every so seriously:
Expect a lot more of these!
Featured Image: 2013/365/52 The Potter Box flickr photo by cogdogblog shared under a Creative Commons (BY 2.0) license A mystery box of electronics sent to me by Grant Potter — hey man, it still works!


@barking From down in the WordPress weeds- I do not know if this feature is listed anywhere, but I discovered that if you activate the WordPress @activitypub.blog plugin, as a bonius your blog is now equipped to autoembed URLs from at least Mastodon and Pixelfed.
Amirite @pfefferle ??
Remote Reply
Original Comment URL
Your Profile