Skip to content

Instantly share code, notes, and snippets.

@janboddez
Last active March 8, 2023 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janboddez/068baa33593e34b9e59e9a78aa0df73d to your computer and use it in GitHub Desktop.
Save janboddez/068baa33593e34b9e59e9a78aa0df73d to your computer and use it in GitHub Desktop.
<?php
add_filter( 'share_on_mastodon_status', function( $status, $post ) {
// Apply `the_content` filters so as to have smart quotes and whatnot.
$status = apply_filters( 'the_content', $post->post_content );
// Strip tags and shorten.
$status = wp_trim_words( $status, 25, ' […]' );
// Prevent double-encoded entities.
$status = html_entity_decode( $status, ENT_QUOTES | ENT_HTML5, get_bloginfo( 'charset' ) );
// Add tags as hashtags.
$tags = get_the_tags( $post );
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 . ' ';
}
// Remove (leading and) trailing spaces.
$status = trim( $status );
}
if ( false === strpos( $status, get_permalink( $post ) ) ) {
// If the permalink is not somehow already part of `$status`, append it.
$status .= "\n\n" . get_permalink( $post );
}
// That's it!
return $status;
}, 99, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment