janboddez / share-on-mastodon

Easily share WordPress posts on Mastodon.
https://jan.boddez.net/wordpress/share-on-mastodon
GNU General Public License v3.0
39 stars 5 forks source link

Support for Hashtags #39

Closed ericdano closed 1 year ago

ericdano commented 1 year ago

It would be great if Tags in Wordpress translated to Hashtags in Mastodon.

janboddez commented 1 year ago

If you're familiar with customizing WordPress (as in, adding custom PHP snippets to either a must-use plugin or your [child] theme's functions.php), you could try something like this:

add_filter( 'share_on_mastodon_status', function( $status, $post ) {
  $tags = get_the_tags( $post->ID );

  if ( $tags && ! is_wp_error( $tags ) ) {
    $status .= "\n\n";

    foreach ( $tags as $tag ) {
      $tag_name = $tag->name;

      if ( preg_match( '/\s+/', $tag_name ) ) {
        // Try to CamelCase multi-word tags.
        $tag_name = preg_replace( '/\s+/', ' ', $tag_name );
        $tag_name = explode( ' ', $tag_name );
        $tag_name = implode( '', array_map( 'ucfirst', $tag_name ) );
      }

      $status .= '#' . $tag_name . ' ';
    }

    $status = trim( $status );
  }

  return $status;
}, 11, 2 );

Also discussed here: https://jan.boddez.net/wordpress/share-on-mastodon#share_on_mastodon_status

janboddez commented 1 year ago

I'm considering adding a bunch of options in a next version, I may take this one along.