POSSE WP->Mastodon

I wish that it were possible to leverage WordPress’ post formats (status, aside, etc.) to trigger crossposting. In the absence of that functionality I’m working with Jan’s excellent WordPress to Mastodon plugin, making a couple of minor mods:

  • If it’s a short (status, under 400 chars) WordPress post, just send the post content to Mastodon with no title or link back to the original wordpress post. If it’s a longer entry, post an excerpt and link back to WordPress.
  • Add tags to the Mastodon post with #’s.

Here’s the code I’m using:

add_filter( 'share_on_mastodon_status', function( $status, $post ) {
    // Check if the length of the post content is more than 400 characters
    if (strlen($post->post_content) > 400) {
        // Use the post excerpt if content is longer than 400 characters
        $excerpt = get_the_excerpt($post);
        // Replace HTML entity with "read more..." text and permalink
        $read_more_link = '... read more: ' . get_permalink($post);
        $excerpt = str_replace('…', $read_more_link, $excerpt);
        $status = wp_strip_all_tags($excerpt, '<a>');
    } else {
        // Use the full post content if it's 400 characters or less
        $status = wp_strip_all_tags($post->post_content);
    }
    
	
  $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 );

I’m going to sit with this for a while. I think the route I’m ultimately going to take though is just modify this plugin to only crosspost to Mastodon entries that have the category of “status” assigned to them. Not sure yet. Will give this a while to settle.

Posted

in