Tweaking the Events Calendar so it displays HTML

Have you noticed that html is missing from Events Calendar excerpts?

The excerpts used in The Events Calendar summaries are a bit lack lustre to me. If like me you want a bit more content and html in your events calendar posts here is a tweak.

The events calendar aggressively trims out html from the page excerpt and reduces it to 55 characters by default. This happens in the following file:

the-events-calendar/src/functions/template-tags/general.php
a screenshot of an Events Calendar except with html missing
No line breaks, bold, italics or links

in this section:

if ( ! has_excerpt( $post->ID ) ) {
	// Temporarily alter the global post in preparation for our filters.
	$global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
	$GLOBALS['post'] = $post;
	
	// We will only trim Excerpt if it comes from Post Content
	
	/**
	* Filter the number of words in an excerpt.
	*
	* @param int $number The number of words. Default 55.
	*/
	$excerpt_length = apply_filters( 'excerpt_length', 55 );
	
	/**
	* Filter the string in the "more" link displayed after a trimmed excerpt.
	*
	* @param string $more_string The string shown within the more link.
	*/
	$excerpt_more = apply_filters( 'excerpt_more', ' […]' );
	
	// Now we actually trim it
	$excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
	
	// Original post is back in action!
	$GLOBALS['post'] = $global_post;
}

This line is what does most of the damage: $excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );

The wp_trim_words() function strips out any html before it does the trimming. It does this, for example, so you don’t end up with a <b>in the middle of your excerpt missing it’s close tag and therefore making everything bold! This makes your events look really dull though 🙁

So, here is what I do. Find the “// Now we actually trim it” comment and replace all the way down to the “// Original post is back in action!” comment with the following:

// Now we actually trim it
// $excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more ); 
$excerpt = force_balance_tags(html_entity_decode(wp_trim_words(htmlentities($excerpt), $excerpt_length, $excerpt_more)));

$excerpt = wp_kses( $excerpt, $allowed_html );

// Original post is back in action!

What’s happening here?

Here, I’m taking the excerpt and before trimming it, converting all the html in it to html entities. This means that the wp_trim_words() doesn’t remove them. Once trimmed by wp_trim_words(), we convert the html entities back to html with html_entity_decode() and then use force_balance_tags() to make sure everything in there that was opened is closed.

Finally, wp_kses() is used to remove any htmls that are in there which aren’t allowed in the standard post content.

Voila!

In theory, you could end up with some funny content in there, if a tag was cut in half so didn’t convert back, but force_balance_tags() should mean that it doesn’t break the page. I’ve not seen it happen yet, but it’s possible.

Annoyingly as this is a change to the core file of the plugin, it is removed every time the plugin upgrades, so I have to keep reimplementing it. There might be a way around that, but I haven’t looked yet!

If 55 is too short for your excerpts, you can use this extra piece of code in your functions.php to grow it:

function change_excerpt_length( $length ) {
	return 100;
}
add_filter( 'excerpt_length', 'change_excerpt_length', 999 );

This will change the excerpt length to 100 characters. Experiment till you’ve got the length that suits your content.

If you want paragraphs and line breaks to appear in the excerpt, you’ll also need to tell WordPress to let them come out. Here is another function for your functions.php

function tribe_add_to_character_filter( $allowed_html ) {

	$base_attrs = array(
		'class' => array(),
		'id' => array(),
		'style' => array(),
	);

	$allowed_html['br'] = $base_attrs;
	$allowed_html['p'] = $base_attrs;
	
	return $allowed_html;
}
add_filter( 'tribe_events_excerpt_allowed_html', 'tribe_add_to_character_filter' );
Bold and italic text, line breaks and a working link

1 thought on “Tweaking the Events Calendar so it displays HTML”

  1. Hi! I submitted my next event with Siren’s Cave which happens on 9th September a week or so ago but it’s still pending. Please could you assist with getting it submitted?

    Many thanks!
    S-J

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top