Source file:
View in GitHubParameters
- $share_url
-
(string) The default message that gets posted to Mastodon.
- $post
-
(WP_Post) The post object.
- $post_data
-
(array) Array of information about the post we’re sharing.
Notes
You can use this filter to customize the default message that is suggested to your readers when they use the Mastodon sharing button on your site.
In the example below, we will use the filter to add a custom message (“I came across … Here is an excerpt: … Read more at …”), as well the post title, the post excerpt, the post link, and the post tags to the message:
/** * Customize the default Mastodon sharing message. * * @param string $message The default message that gets posted to Mastodon. * @param WP_Post $post The post object. * * @return string The customized message. */ function jeherve_custom_mastodon_message( $message, $post ) { /* * Let's build some basic information about the post. * Post title, link, excerpt. */ $post_title = get_the_title( $post ); $post_link = get_permalink( $post ); $post_excerpt = get_the_excerpt( $post ); /* * Let's build an array of tags. * We'll massage the output a bit to add hashtags in front of each tag. * We'll capitalize the first letter of each word, for better accessibility. */ $post_tags_array = get_the_tags( $post ); if ( ! $post_tags_array ) { $post_tags_array = array(); } else { $post_tags_array = array_map( function ( $tag ) { // Camel case the tag name and remove spaces as well as apostrophes. $tag = preg_replace( '/\s+|\'/', '', ucwords( $tag->name ) ); // Return with a '#' prepended. return '#' . $tag; }, $post_tags_array ); } // Build a string of tags. $post_tags = implode( ', ', $post_tags_array ); /* * And now we build our final message. * This is of course just an example, you can build any output you want. * * For example, once Mastodon supports Markdown, * you could use Markdown to build a really fancy-looking message. * @see https://github.com/mastodon/mastodon/issues/18958 */ return sprintf( 'I just came across %1$s. Here is an excerpt: %2$s -- Read more at %3$s %4$s', esc_html( $post_title ), esc_html( $post_excerpt ), esc_url( $post_link ), esc_html( $post_tags ) ); } add_filter( 'jetpack_sharing_mastodon_default_message', 'jeherve_custom_mastodon_message', 10, 2 );