• [email protected]

    (@mathewmathewingramcom)


    Hi Jan — I’ve been using the plugin for some time without incident, and then I tried to crosspost a normal blog post and the plugin said “Validation failed: Text character limit of 500 exceeded.” I tried reformatting, and also reset the settings on the plugin and then uninstalled and reinstalled the plugin but am still getting the error

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Jan Boddez

    (@janboddez)

    Have you by any chance ever set up a filter callback? Something like:

    add_filter( 'share_on_mastodon_status', function( $status, $post ) {
      $status = wp_strip_all_tags( $post->post_content );
      return $status;
    }, 10, 2 );

    By default, only a title + permalink is posted on Mastodon, but if you’ve set up this filter, then that may no longer be true.

    The trick, then, is to only have the filter callback affect (short) notes (or whichever post type you’re targeting). Reason I bring this up is you mention “regular posts,” so that sort of sounds like you’ve been crossposting a different post type?

    If so, do you know the “name” of the (shorter) post type? (The post_type bit in URLs like wp-admin/edit.php?post_type=indieblocks_note.) I could help you adapt the code snippet.

    Previous versions of the plugin would not show the error (but also not post anything to Mastodon). Could be another reason you’re only seeing this now.

    Thread Starter [email protected]

    (@mathewmathewingramcom)

    I have been using a function/filter, yes — to add more than just a headline and link. I used code from this site: https://shkspr.mobi/blog/2022/11/better-sharing-of-wordpress-posts-to-mastodon/

    I don’t have different categories of post, but I did try to edit the filter to remove the posting of the title. Perhaps I screwed it up somehow ?? PHP is not my forte.

    What seemed strange is that some posts would publish on Mastodon just fine, and others would produce the error, but I couldn’t see any difference between those that did and those that didn’t.

    Plugin Author Jan Boddez

    (@janboddez)

    Well, it would seem that, at least for certain (?) posts, the resulting status (maybe because of the excerpt or so?) is over 500 characters, and then Mastodon will refuse it.

    You could try removing the filter completely, or tweaking it so that statuses are always under 500 characters.

    There is, in fact, a separate filter that will bluntly cut off the excess characters (but it will almost certainly screw up your permalink or tags).

    It’s best to handle this in the share_on_mastodon_status that you already have/had.

    Thread Starter [email protected]

    (@mathewmathewingramcom)

    Thanks — I reverted to the original filter and it seems to work now. But it has always auto-truncated the post when sharing to Mastodon, so I still don’t know why I sometimes got the error and other times not. But thanks for your feedback!

    Plugin Author Jan Boddez

    (@janboddez)

    Absolutely!

    The error, just for clarity, is not generated inside the plugin (or WordPress) but is instead returned by your Mastodon instance. So it’s almost certain that whatever went through was indeed too long. But it entirely depends on what’s in that filter; the plugin by default “toots” only a title and permalink.

    Glad it’s working again; if you put your function on a GitHub gist or similar, I might have a look still.

    Thread Starter [email protected]

    (@mathewmathewingramcom)

    Thanks, Jan — I assumed it would auto-truncate the post regardless of how many characters it was, and indeed it seems to be doing that, so not sure why that error would show up sometimes and not other times. I don’t use excerpts in WordPress so it is just grabbing the post and making its own excerpt I assume. The filter or function is this:

    add_filter( ‘share_on_mastodon_status’, function( $status, $post ) {
    // Create a short preview of the post
    $status = “New blog post\n\n”;
    $status .= “\”” . get_the_title($post) . “\”\n\n”;
    $status .= get_the_excerpt($post);
    // Remove the … forced by the excerpt and replace with the Unicode symbol
    $status = html_entity_decode($status);
    // Add a link
    $status .= “\n\nRead more: ” . get_permalink( $post );
    // Add tags
    $tags = get_the_tags( $post->ID );
    if ( $tags ) {
    $status .= “\n\n”;
    foreach ( $tags as $tag ) {
    $status .= ‘#’ . preg_replace( ‘/\s/’, ”, $tag->name ) . ‘ ‘;
    }
    }
    $status = trim( $status );
    return $status;
    }, 10, 2 );

    Plugin Author Jan Boddez

    (@janboddez)

    I think the auto-truncating you’re seeing is WordPress’ built-in excerpt generation. It can be tweaked to return fewer words. You could also try to drop bits from the output. (Maybe you’ve got a ton of tags on these posts, for instance.)

    The plugin by default does not truncate statuses.

    So some combination of the above is pushing statuses over 500 characters, causing the server to reject them.

    Possible tweaks (untested, I’m afraid, but it may get you going):

    add_filter( 'share_on_mastodon_status', function( $status, $post ) {
    	// Shorten excerpts
    	add_filter( 'excerpt_length', function() {
    		return 20;
    	}, 998 );
    
    	// Create a short preview of the post
    	$status  = '"' . get_the_title($post) . "\"\n\n";
    	$status .= get_the_excerpt($post);
    
    	// Back to default.
    	add_filter( 'excerpt_length', function() {
    		return 55; // Default value; might want to change this if you've set a different value elsewhere
    	}, 999 );
    
    	// Remove the … forced by the excerpt and replace with the Unicode symbol
    	$status = html_entity_decode($status);
    
    	// Add a link
    	$status .= "\n\n" . get_permalink( $post );
    
    	// Add tags
    	$tags = get_the_tags( $post->ID );
    
    	if ( $tags ) {
    		$status .= "\n\n";
    
    		foreach ( $tags as $tag ) {
    			$status .= '#' . preg_replace( '/\s/', '', $tag->name ) . ' ';
    		}
    	}
    
    	$status = trim( $status );
    
    	return $status;
    }, 10, 2 );
    Thread Starter [email protected]

    (@mathewmathewingramcom)

    Thanks Jan — I’m going to try some or all of those suggestions. And thanks again for taking the time to help me, I really appreciate it!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Plugin says character limit exceeded every time’ is closed to new replies.