Created
November 18, 2025 03:16
-
-
Save danieljwonder/acf9fd5f1d9ecf25b89f49294d6debf8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: Customise 𝕏 Content for Auto Share for Twitter | |
| * Description: Modifies Auto Share for 𝕏 to use post content instead of title and removes the URL for Notes. | |
| * Version: 1.0.0 | |
| * Author: Daniel Wonder | |
| */ | |
| // If this file is called directly, abort. | |
| if ( ! defined( 'WPINC' ) ) { | |
| die; | |
| } | |
| /** | |
| * Replaces the tweet body with the post content. | |
| * | |
| * @param string $tweet_body The original tweet body. | |
| * @param WP_Post $post The post object. | |
| * @return string The modified tweet body. | |
| */ | |
| function custom_tweet_body( $tweet_body, $post ) { | |
| // Only modify for 'note' post type | |
| if ( 'note' !== $post->post_type ) { | |
| return $tweet_body; | |
| } | |
| // Get the post content and strip shortcodes | |
| $content = strip_shortcodes( $post->post_content ); | |
| // Convert <p> and <br> tags to double line breaks, then strip all other HTML | |
| $content = wpautop( $content, false ); | |
| $content = wp_strip_all_tags( $content, false ); | |
| // Convert double line breaks to single newlines (Twitter displays these as line breaks) | |
| $content = preg_replace( "/\n\s*\n/ms", "\n\n", $content ); | |
| // Clean up any remaining whitespace | |
| $content = trim( $content ); | |
| // Truncate if needed, trying to preserve paragraphs | |
| $max_length = 275; // Max tweet length with room for ellipsis | |
| if ( strlen( $content ) > $max_length ) { | |
| $content = force_balance_tags( htmlspecialchars_decode( $content ) ); | |
| $content = mb_substr( $content, 0, $max_length ); | |
| $content = preg_replace( '/\s+\S*$/', '…', $content ); | |
| } | |
| return $content; | |
| } | |
| add_filter( 'autoshare_for_twitter_body', 'custom_tweet_body', 10, 2 ); | |
| /** | |
| * Removes the URL from the tweet. | |
| * | |
| * @param string $url The original URL. | |
| * @param object $post The post object. | |
| * @return string An empty string to remove the URL. | |
| */ | |
| function remove_tweet_url( $url, $post ) { | |
| // Only remove URL for 'note' post type | |
| if ( 'note' !== $post->post_type ) { | |
| return $url; | |
| } | |
| return ''; | |
| } | |
| add_filter( 'autoshare_for_twitter_post_url', 'remove_tweet_url', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment