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

Tags in Posts #23

Closed Danie10 closed 2 years ago

Danie10 commented 2 years ago

I see this was opened about 2 years ago and closed as a possible option. Mastodon, like Twitter, is actually driven largely by the hashtags in the posts, so would be a very useful option to be added to the plugin. Is there any update on this, as not everyone wants to venture into editing their theme?

Apart from this, the plugin really works well, and it's a pity it is missing.

janboddez commented 2 years ago

Is there any update on this, as not everyone wants to venture into editing their theme?

Does that mean you, in principle, agree to the current approach, but would prefer an extra setting or so to "just" toggle this on or off?

As a side note: an alternative to adding to functions.php (and possibly losing your changes with a theme upgrade) are mu-plugins. They're simple PHP files that you can just drop in the wp-content/mu-plugins folder.

Danie10 commented 2 years ago

Thanks for that super quick response. Yes always in favour of options, so a toggle would be good. I already include a # in front of my tags so mine would convert as-is, but I know many just use plain tag words which could require a # to be prepended (so again maybe an option.

OK that drop-in for the mu-plugins sounds like it is worth giving it a go. I'd have to go see exactly how that needs to be done, but the approach does sound better than editing the theme.

Danie10 commented 2 years ago

Thanks actually the mu-plugin idea worked perfectly. It added the extra # but I just modified the code by removing the "#" which will sort it out from the next post.

For others, I did this:

  1. Created a directory called mu-plugins under wp-content (if it is not there)
  2. Created a text file called mastodon-tags.php (you can use any name)
  3. Pasted in the following:

    
    <?php
    add_filter( 'share_on_mastodon_status', function( $status, $post ) {
    $tags = get_the_tags( $post->ID );
    
    if ( $tags ) {
    $status .= PHP_EOL . PHP_EOL;
    
    foreach ( $tags as $tag ) {
      $status .= '#' . preg_replace( '/\s/', '', $tag->name ) . ' ';
    }
    
    $status = trim( $status );
    }
    
    return $status;
    }, 10, 2 );

Just make sure the file and group owner is the same as your other files there, e.g. usually www-data.

Can confirm the plugin is seen by going to WP Dashboard and Plugins. A new tab link should appear that the top called  'Must-Use'.

If your tags on WP include a # already, you can modify the code by changing '#' to just '' (remove the #).
janboddez commented 2 years ago

If your tags on WP include a # already, you can modify the code by changing '#' to just '' (remove the #).

Good remark! I incorrectly assumed tag names contained only alphanumeric and whitespace characters, which is obviously not true. Should probably change the snippet a bit (or the actual code, if it ever ends up making it into the plugin) to take that into account. (On my local install, I also CamelCase multi-word tags, which is generally considered good for accessibility.)