Closed jasontucker closed 1 year ago
Very nice. Maybe a small typo where it says indieblocks_notes
rather than indieblocks_note
? Also I think for regular posts you might be appending to the existing status rather than replace it.
I now use something like this (slightly modified and thus "untested").
TL;DR: Notes get their entire status overwritten by the post's content. If I happen to have a Markdown source, I use that. I also remove Jetpack’s location stuff (if applicable). Lastly, for all post types, I add tags and a permalink (if there isn't one, yet, which is only going to be the case for notes).
add_filter( 'share_on_mastodon_status', function( $status, $post ) {
if ( 'indieblocks_note' === $post->post_type ) { // Or whatever your note type would be.
// Post types that use Markdown (through Jetpack) have their raw content
// stored in `post_content_filtered`.
if ( '1' === get_post_meta( $post->ID, '_wpcom_is_markdown', true ) ) {
$content = $post->post_content_filtered;
}
if ( empty( $content ) ) {
// Fall back to `post_content`.
$content = $post->post_content;
}
// Prevent Jetpack from appending geolocation data.
if ( class_exists( 'Jetpack_Geo_Location' ) ) {
$jp_geo_loc = Jetpack_Geo_Location::init();
remove_filter( 'the_content', array( $jp_geo_loc, 'the_content_microformat' ) );
}
// Apply `the_content` filters so as to have smart quotes and whatnot.
$status = apply_filters( 'the_content', $content );
// Strip tags.
$status = wp_strip_all_tags( $status );
}
// 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 ) ) ) {
// Append permalink.
$status .= "\n\n" . '(' . get_permalink( $post ) . ')';
}
// That's it!
return $status;
}, 99, 2 );
Almost forgot! Even more inspiration: https://shkspr.mobi/blog/2022/11/better-sharing-of-wordpress-posts-to-mastodon/
Since the notes have a generated title and I had my notes tooted I was getting redundant titles. I made this modification maybe it may be useful.
`add_filter( 'share_on_mastodon_status', function( $status, $post ) { // Create a short preview of the post
}, 10, 2 );`