// Make sure Activitypub services embed videos
// plugin will skip embedding videos if it detects a feat. image
// @param obj $ap_obj An object array of the post for Activitypub to use
add_filter('activitypub_activity_object_array', function($ap_obj){
// The "id" key is actually the URL/permalink of the post, not the post ID.
// Get post ID from the permalink
$post_id = url_to_postid($ap_obj['id']);
// Check if a #video micropost
if (has_tag('video', $post_id) && has_category('microblog', $post_id)) {
// Remove any media attachments
$ap_obj['attachment'] = [];
}
return $ap_obj;
}, 100);
This code assumes you categorize and tag your posts the same way I do (“Microblog” for as the category, and “video” for tag). You might want to change it to fit your own system.
Change this line: if (has_tag('video', $post_id) && has_category('microblog', $post_id)) {
The Activitypub Object Array?
The filter activitypub_activity_object_array is used to modify the object array of the post. This is completely different from WP_Post. You can see the object array of each post by appending /activitypub to a post. Ex. https://test.giantpaper.org/microblog/10766/200-kids-sing-civilization-theme/activitypub/(source)
Background
I have a lot of “video” type posts, where the main content is a single video. If the post contains a “post thumbnail”, the Activitypub plugin automatically considers it media (along with any other images you might have added).
In video posts, this is usually the poster image/thumbnail/preview image of the video. Which would be great, EXCEPT I want the video itself (as in, actually embedded) to be the main feature, not the video thumbnail. The above code removes the featured image from the media array, so the video gets embedded instead.
Ever since the Great Federating of Giantpaper, I’ve been thinking about how I would get Activitypub to correctly display the different types of blog posts I have set up. Ex: this post, which looked like this on Mastodon:
There were some things I learned about WP posts ending up on the Fediverse through this plugin:
Custom fields (like those generated by Advanced Custom Fields) don’t get included, so links from my linklog posts won’t appear (which is why this post was posted as a micropost instead of a linkpost)
Most HTML gets stripped out by some Fedi services like Mastodon, which is why videos weren’t appearing on microposts
The Activitypub plugin has a setting for tweaking the template that gets used for federation:
…but there’s no template tag for custom fields (and no way to not have the plugin render video URLs as video embeds).
Enter Filters
I saw that there was a PR for making the post template filterable, that was upcoming for v2.0.0. And against my judgement, I jumped at it when it released, thinking there would be some sort of documentation for the new filters, but NOPE. I didn’t even know what the filter was called!! đŹ I did see the addition of a new filter name in v2.0.0.0, which is what I thought it was going to be, but actually trying to use it to filter stuff got me the dreaded “Your author URL does not return valid JSON for application/activity+json. Please check if your hosting supports alternate Accept headers.” error in WordPress’s Site Health page. And also I (somehow) found that outputting the contents of $template from the first parameter that it was actually meant to alter the post template (from the screenshot above with the [ap_....] tags), but I’m trying to figure out how to modify the HTML output of [ap_content]. So that wasn’t it.
(FYI, the filter name wasn’t what I was looking for, but I did find a Very Important Piece of info in the linked thread that helped me debug my code later. More on that later.)
AND THEN, the search bar at the top, I searched for apply_filters (because if there’s a new way to filter content, it for sure would be added to the code via apply_filters()):
Here is my (very unofficial) documentation for this:
/**
*
* @param string $content The outputted HTML of [ap_content]
* @param WP_Post $post The post object -- see https://developer.wordpress.org/reference/classes/wp_post/ and https://developer.wordpress.org/reference/functions/get_post/ for more info
*
*/
add_filter('activitypub_the_content', function($content, $post){
// add code here
return $content
}, 100, 2);
First the videos…
This is just some normal non-WP specific code. So if you’re already familiar with PHP, you might already know this. But to make sure your videos show up in your fediposts:
You’re basically removing the iframes and surrounding <figure> and <div> tags from around the videos and reformatting the embed URLs (inside the src="" attribute) back to their web accessible URLs (so https://www.youtube.com/embed/t476sB13EOg â https://www.youtube.com/watch?v=t476sB13EOg). And then linking to themselves, which will make Mastodon (and maybe other fediservers) think “oh hey, this is a link, let’s put up a preview”, and embeds the video.
So from this:
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper"><iframe loading="lazy" title="The Prince of Peace" width="500" height="375" src="https://www.youtube.com/embed/AQZqaz10TOM?feature=oembed" frameborder="0" allow="web-share" allowfullscreen></iframe></div></figure>
I’ve finally done it. đŹ Ever since I heard that it was possible to federate WordPress blogs, I’ve been interested in having GPORG join the fediverse. For me, it wasn’t just plug ‘n play, because of my setup (Litespeed Web Server, with a cache plugin + Cloudflare). WordPress’s site health thing kept saying:
Your author URL does not return valid JSON for application/activity+json. Please check if your hosting supports alternate Accept headers.
Apparently, you can test whether or not these accept headers are working by entering in the command line:
With the https://test.giantpaper.org/author/giantpaper/ being the URL to your author page. It’s supposed to bring up a normal HTML page just as you designed it when viewed in the browser, but when entered in the command line with the accept header, it’s supposed to return JSON. For me, it was returning as text/html, which is what the problem was.
Here’s what I did. Hopefully it’ll help someone else.
Caches
Every tutorial or fix I saw online was either for Apache or nginx, but I’m using Litespeed. Supposedly, caches aren’t supposed to butt heads with the Activitypub plugin, but there were some exceptions (some people found it started working after they disabled plugins like Jetpack). I figured out I could bypass the cache by adding https://test.giantpaper.org/author/giantpaper/ to the exclude list. Only problem is that the cache would also be bypassed if the page were accessed in browser which I didn’t want.
I’m using my own version of Sage and found that the accept header thing works if I had the default Twentytwentyfour theme activated. I found that the correct JSON code was returning with my own custom theme, but it had the surrounding <html> and <body> tags, as well as everything in the <head> tag everything outside the #app container, which what I have in my index.php file for the theme. I think it had something to do with all of this code being in the index.php file (normally this would be in the header.php and footer.php files). Wasn’t sure how to not make it do that other than:
(The $_SERVER['HTTP_ACCEPT'] == 'application/activity+json' tells the server to only bring up this code if the content-type is set to application/activity+json. Since the content-type of WordPress posts & pages are almost always text/html when viewed in a browser, you won’t be smacked with a big wall of weird-looking code when trying look at your own author page.)
I put this at the very top of my index.php file. If you do:
You should see a big wall of confusing looking code (compressed JSON). This is what you want.
Cloudflare (optional?)
I’m not sure if this was necessary, because supposedly the rewrite rule from the Caches section would’ve taken care of this, but I did the previous two items and it still wasn’t working. So in Cloudflare, under Caching > Cache Rules, I added a new rule:
URI Path: Not sure if it really needed to be URI Path or if URI or URI Full would’ve sufficed. Obviously if you use URI Full, you need to include the domain name + protocol (https://…).
Request Headers: In the blank field after Request Headers, put content-type. Field #3, put as equals. Field #4 as application/activity+json
Browser Cache TTL
I found I had to leave the Browser TTL as 4 hours (which I think is the default):
Disclaimer
Earlier, whenever I clear the site cache, I notice I would get the same message in Site Health:
Your author URL does not return valid JSON for application/activity+json. Please check if your hosting supports alternate Accept headers.
Sidenote: I updated something on the site and cleared the cache. I don’t get this message anymore. Why? I don’t know.
Sometimes you might notice that the error message won’t go away after doing any of the above. I THINK the Site Health checker just needs time to check again (a half an hour? An hour?). I’m not sure if it’s a cache problem.
What it doesn’t do
If you have custom fields on your theme, you won’t be able to include them in fedi posts. Hopefully that will change in the future, because my linklog posts make so sense without the external URL.
This is completely separate from Lemmy or Mastodon, and there’s no way to connect your existing accounts with your newly federated WordPress site. It creates a completely separate profile on all supported fedinetworks.
Looks like it doesn’t support kbin yet. Kbin is still new and doesn’t have an API released at the moment (this is being worked on), so…yeah.
Any new posts you create will federate, but it won’t pull over pre-federation posts. It’s most like email than say RSS.
Just like with a lot of video games these days, there’s the initial release that everyone’s hyped about. Then as you’re playing it, you discover some glaringly obvious, annoying bugs that should’ve been fixed during playtesting. This was the case!
Fixed:
MOBILEâNot being able scroll all the way down on the main menu.
MOBILEâTweaked the main menu so the close animation is smoother.
I’ve made some improvements!
I’ve semi-decorated the front page with Christmas (changed the hero image) so I decided to go all the way and change the colors to be more holidayish.
I’ve added shortlinks!
WordPress already has support for shortlinks, but they’re just the default /?p=[post ID] which is the permalink structure of posts & pages if pretty permalinks are disabled. Shortlinks are available on all posts except the linklog, since the “permalink” is kind of supposed to be the external URL anyway (and there’s no way to shorten that with the plugin).