Post updated* : 27/02/2023
This blog is federated, so if you follow it from your Mastodon server, you’ll see everything posted here on your home timeline. Write in the search box of your Mastodon app the URL https://www.blogpocket.com/author/antonio
(*) The following problem has already been solved: We need the URLs of the posts of a federated WordPress blog to be recognizable and capable of being found in the search box. The permanent links are not found, but the URL with the native mode ID (www.blogpocket.com/?p=<ID>) is, where <ID> is the identification number of the post.
This article describes a way to prompt the readers of a blog (eg Blogpocket 😉 ) to reply to your posts, from the corresponding Mastodon post. To do this, the WordPress page ID is used and a PHP code is injected into the individual post template. Once the post is published, this code allows you to provide a direct URL to do so, of the type www.blogpocket.com/?p=<iD>, where <ID> is replaced by the identification number of the page. Mastodon is able to find URLs of that type in searches, so the problem is solved.
The problem
The WordPress ActivityPub plugin allows you to publish blog posts on Mastodon, making them visible on the home timeline for all those users who follow it. To do this, it is necessary to know the user of the federated blog in Mastodon which, for example, for the Blogpocket blog is @acambronero. Remember that the corresponding URL would be https://www.blogpocket.com/author/antonio; that is, something that does not match the structure of URLs in Mastodon that are of the style https://<host>/@<username>. The good thing about the ActivityPub plugin is that if a Mastodon user follows the federated blog, they can reply to any status visible on the home timeline and that reply will be reflected in the federated blog comment list, along with the rest of the comments made via the blog. native WordPress system. Even if you don’t follow it, you can paste the URL in the search box and reply in the same way.
What we want to add, on each page of individual WordPress blog posts, is a system that provides blog readers with a specific Mastodon URL so they can comment on the post from there. The ActivityPub plugin will do its job and reflect the response in the post’s comment list.
The idea
From this post by Carl Schwan (Adding comments to your static blog with Mastodon), I wondered if it would be easy to implement that little piece of code so that my readers could comment on my WordPress blog posts (Blogpocket.com) from Mastodon.
Carl Schwan has it running in his Hugo’s static blog. But, a priori, the implementation should not be that different. Would it be worth installing that system on a WordPress blog?
Comments on your federated blog thanks to ActivityPub
ActivityPub has the ability to bring responses to posts on Mastodon to your WordPress federated blog’s comment list. This is basically why Carl Schwan’s system is not worth installing on WordPress.
However, the first part of the script in which the user is offered an invitation message to comment on the post in their Mastodon account is very interesting. In addition, to facilitate it, the URL of the post in Mastodon is included (to copy to the clipboard). This allows you to paste the URL into the search box of the Mastodon app used and get to the post directly in order to reply.
Considerations
One drawback is that Mastodon searches do not work for URLs assigned by the ActivityPub plugin to the statuses associated with federated blog posts.
For example, if the state has an ID, the URL https://www.blogpocket.com/author/antonio/ID does not work when looking for it (assuming https://www.blogpocket.com/author/antonio is the Mastodon user URL, corresponding to the federated WP blog)
But I have found the solution to the problem of finding the Mastodon URL of a published state using the WordPress ActivityPub plugin. Actually, WordPress assigns an ID to each post and that makes up a unique URL.
However, the important thing is:
- We are able to offer, within the individual post page of the federated blog, a specific URL to search on Mastodon.
- Any user, on any Mastodon server, can follow the federated blog posts on their home timeline.
- In addition, those users can respond from there and the responses will automatically be reflected in the federated blog posts, along with the rest of the comments made through the native WordPress system.
- And, the readers of the federated blog, who do not follow it in their Mastodon accounts, will be able to locate the URL to comment from Mastodon.
Implementation
To implement the warning message in WordPress, simply Carl Schwan’s code served as inspiration but my development is basically based on the use of the function get_the_id();
and the Insert plugin PHP Code Snippet; plus the block mechanisms of the site editor in WordPress (Gutenberg)
For all purposes, as the WordPress blog is federated, the host is www.blogpocket.com (or whatever domain it is), Username is not needed and we simply require the ID that we will obtain with get_the_id();
With the Insert PHP Code Snippet plugin we will inject the PHP code (which you can see a bit further down) into the single.php template of the active WordPress theme. This plugin associates a shortcode to the created snippet, which can be included in the template through the Shortcode block.
Once the post has been published on the blog and replicated on Mastodon; everything will be ready.
Carl Schwan’s system is based on JavaScript and the platform is static like Hugo. However, we have the site editor and Gutenberg blocks in WordPress. My implementation builds on this and therefore rules out coding no javascript, using only PHP and editor blocks. Of course, everything can be improved and, even, Javascript could be used in order to optimize the design. But I do not see the latter as necessary, if what we are after is simplicity and minimalism.
PHP code
The PHP code is as follows (based on Carl Schwan’s script)
<?php
$num = get_the_id();
$username = get_field( 'username' );
if( !empty( $num ) ) {
echo '<div style="background-color: #E6E7E9; font-size: 25px; padding: 20px; padding-top: 10px;">';
echo '<p>You can use your Mastodon account to reply to this <a class="link" href="https://www.blogpocket.com/?p='. $num . '">post</a > from ' . $username .'</p>';
echo '<p>Copy and paste the URL into the search field of your Mastodon server's web interface. </p></div>
';
} else {
echo '<p>URL not available. You can search for this post by following the user @acambronero@www.blogpocket.com on Mastodon</p>';
}
?>
This code is improvable. The style should be included in the style.css file or in Additional CSS. The method for getting the URL structure should also be optimized.
But the important thing about this snippet is that it is capable of composing the URL, valid for Mastodon, of each entry, with the $username and $num. An example URL would be www.blogpocket.com/?p=1111.
The snippet is saved thanks to the Insert PHP Code Snippet plugin, and injected into each blog post using the Shortcode block (each snippet created with the Insert PHP Code Snippet plugin is assigned a shortcode).
An accordion block is used to simulate the pop-up of Carl Schwan’s script.
For usability reasons, it was decided to invite the user to copy the link through the option to copy it from the contextual menu associated with the right mouse button. It is true, perhaps, that for this operation the copy button from Carl Schwan’s javascript would be useful, but I think it is not so necessary.
Conclusions
This is not an alternative to native WordPress comments, but rather something complementary. If the post is replied to on Mastodon, there is a better chance that that reply will be seen on the timeline of people who follow you and thus could discover the blog post.
[This post was originally published (in Spanish) on Blogpocket.com.]
Leave a Reply