
I’ve been using Jan Boddez his Share on Mastodon plugin and really like how it works. Simple and clean. But the toots are not formatted very well. Poking through his code I found he added a filter for it so you can make your own format. Excellent.
Grab a copy of the plugin here: https://wordpress.org/plugins/share-on-mastodon/. It’s a super simple plugin to install and use.
Over the past few days I’ve been making test posts and settled on a format for toots like so: https://home.social/@arnandegans/109684410554812887.
Clean, simple, easy to read.
If you want this layout as well, add the following code to your themes functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Reformat the toot format/template for Share to Mastodon function ajdg_share_on_mastodon($status, $post) { $formatted_status = wp_strip_all_tags(html_entity_decode(get_the_title($post->ID), ENT_QUOTES | ENT_HTML5, get_bloginfo('charset'))) . "\n\n"; $formatted_status .= mb_substr(wp_strip_all_tags(html_entity_decode(get_the_excerpt($post->ID))), 0, 199, get_bloginfo('charset')) . "[…]\n"; $formatted_status .= esc_url_raw(get_permalink($post->ID)) . "\n\n"; $toot_tags = get_the_tags($post->ID); if($toot_tags) { foreach($toot_tags as $tag) { $tags[] = "#" . preg_replace("/[^a-z0-9]/i", '', $tag->name); } $formatted_status .= implode(" ", $tags); unset($tags, $toot_tags); } return $formatted_status; } add_filter( 'share_on_mastodon_status', 'ajdg_share_on_mastodon', 10, 2); |
This basically does the same as the plugin does by default. But adds the except of the post (or a snippet if the excerpt doesn’t exist) and adds the tags. Tags with spaces do not work on Mastodon, so spaces are stripped out. And a few new lines are added for readability.
Enjoy!
Thank you – that worked like a charm, using the photo, the excerpt, the URL, the tags, AND the alt text for the photos – success at last.
For anyone who doesn’t normally mess around under the hood, I always recommend making a copy of the original file you’re editing as a plain text file (just in case you need to revert!) and then add the above code and save.
Yep, making backups is always a good idea.
Glad you find the post helpful.