• Resolved gloriousnoise

    (@gloriousnoise)


    Hi! Love your plugin and have been using it and it’s been working great. One weird thing I noticed today is that if I schedule a wordpress post to publish at a later date, when it’s shared on mastodon it doesn’t include my tag elements. If I unlink it and update the wordpress post (without changing anything), the new masto post includes the tags. Weird, right?

    Here’s my addition to my theme’s functions.php:

    add_filter( 'share_on_mastodon_status', function( $status, $post ) {
    
    	$toot_size = (int) 500;
    
    	//$message_template = "[title]\n\n[excerpt]...\n\nRead more: [permalink]\n\n[tags]";
    	$message_template = "[excerpt]...\n\nRead more: [permalink]\n\n[tags]";
    
    	//Replace title
    	//$post_title = html_entity_decode(get_the_title($post), ENT_COMPAT, 'UTF-8');
    	//$message_template = str_replace("[title]", $post_title, $message_template);
    
    	//Replace permalink
    	$post_permalink = get_permalink( $post );
    	$message_template = str_replace("[permalink]", $post_permalink, $message_template);
    
    	//Replace tags
    	$post_tags_content = '';
    
    	$post_tags = get_the_tags($id);
    	if ($post_tags) {
    		foreach ($post_tags as $tag) {
    			$post_tags_content = $post_tags_content . '#' . preg_replace('/\s+/', '', html_entity_decode($tag->name, ENT_COMPAT, 'UTF-8')) . ' ';
    		}
    		$post_tags_content = trim($post_tags_content) . ' #music #blog';
    	}
    	$message_template = str_replace("[tags]", $post_tags_content, $message_template);
    
    	//Replace excerpt
    	//Replace with the excerpt of the post
    	$post_optional_excerpt = $post->post_excerpt;
    	if (strlen($post_optional_excerpt) > 0) {
    		$post_content_long = $post_optional_excerpt;
    	} else {
    		$post_content_long = $post->post_content;
    	}
    
    	$post_content_long = strip_shortcodes($post_content_long);
    	$post_content_long = html_entity_decode($post_content_long, ENT_COMPAT, 'UTF-8');
    	$post_content_long = wp_strip_all_tags($post_content_long);
    
    	$excerpt_len = $toot_size - strlen($message_template) + 27 - 5; // + 9 - 5;
    
    	mb_internal_encoding("UTF-8");
    
    	$post_excerpt = mb_substr($post_content_long, 0, $excerpt_len);
    
    	$message_template = str_replace("[excerpt]", $post_excerpt, $message_template);
    
    	
        $status = trim( $message_template );
        return $status;
    }, 10, 2 );
    
    //add_filter( 'share_on_mastodon_toot_args', function( $args ) {
    //  $args['spoiler_text'] = 'New on Glorious Noise...';
    //  return $args;
    //} );
    
    add_filter( 'share_on_mastodon_toot_args', function( $args, $post = null ) {
      $args['spoiler_text'] = 'New on Glorious Noise...';
    
      if ( $post ) {
        $args['spoiler_text'] = html_entity_decode(get_the_title( $post ));
      }
    
      return $args;
    }, 10, 2 );
    

    I’m guessing it has something to do with my get_the_tags($id). Does that not exist on scheduled posts?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter gloriousnoise

    (@gloriousnoise)

    Ha! Changing it to get_the_tags($post) seems to have solved the issue!

    Plugin Author Jan Boddez

    (@janboddez)

    Yes, in your exact code example above, $id is undefined, and no tags will be returned. As you’ve already indicated, $post should work, as should $post->ID (either is fine, there’s no difference at all).

    Doesn’t explain why it seemed to work before or when posting immediately, though (I wouldn’t know either, as the exact same code is run in both cases), but I’m glad it’s working!

    Plugin Author Jan Boddez

    (@janboddez)

    Oh, I think I know! When the first argument passed to get_the_tags() is undefined (or null), it defaults to the post being edited (the global $post variable). For scheduled posts, this too will be null.

    Either way, good to hear it’s fixed.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Difference with scheduled post?’ is closed to new replies.