Sharing WordPress Posts on Mastodon

There’s a plugin for that -> Share on Mastadon

Terrence Eden proposed the following filter to make better-looking posts to Mastodon:

add_filter( 'share_on_mastodon_status', function( $status, $post ) {
    //  Create a short preview of the post
    $status = "New blog post: ";
    $status .= "\"" . get_the_title($post) . "\"\n\n";
    $status .= get_the_excerpt($post);
    //  Remove the … forced by the excerpt and replace with the Unicode symbol
    $status = str_replace("…", "…", $status);
    //  Add a link
    //$status .= "\n\nRead more: " . get_permalink( $post );
    //  Add tags
    $tags = get_the_tags( $post->ID );
    if ( $tags ) {
        $status .= "\n\n";
        foreach ( $tags as $tag ) {
            $status .= '#' . preg_replace( '/\s/', '', $tag->name ) . ' ';
        }
    }
    $status = trim( $status );
    return $status;
}, 10, 2 );

Add this code to your theme in wp-content/themes/themename/functions.php and make sure auto-update is turned off for that theme.

dlk

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.