tribe_events_get_the_excerpt( WP_Post|int|null $post = null, array $allowed_html = null, boolean $skip_postdata_manipulation = false )

A Excerpt method used across the board on our Events Plugin Suite.

By default it removes all shortcodes, the reason for this is that shortcodes added by other plugins/themes may not have been registered by the time our ajax responses are generated. To avoid leaving unparsed shortcodes in our excerpts then we strip out anything that looks like one.


Parameters

$post

(WP_Post|int|null) (Optional) The Post Object|ID, if null defaults to get_the_ID()

Default value: null

$allowed_html

(array) (Optional) The wp_kses compatible array

Default value: null

$skip_postdata_manipulation

(boolean) (Optional) Defaults to false. When true, the resetting of global $post variable is disabled. (Useful for some contexts like month view.)

Default value: false


Top ↑

Return

(string|null) Will return null on Bad Post Instances


Top ↑

Source

File: src/functions/template-tags/general.php

	function tribe_events_get_the_excerpt( $post = null, $allowed_html = null, $skip_postdata_manipulation = false ) {
		// If post is not numeric or instance of WP_Post it defaults to the current Post ID
		if ( ! is_numeric( $post ) && ! $post instanceof WP_Post ) {
			$post = get_the_ID();
		}

		// If not a WP_Post we try to fetch it as one
		if ( is_numeric( $post ) ) {
			$post = WP_Post::get_instance( $post );
		}

		// Prevent Non usable $post instances
		if ( ! $post instanceof WP_Post ) {
			return null;
		}

		// Default Allowed HTML
		if ( ! is_array( $allowed_html ) ) {
			$base_attrs = array(
				'class' => array(),
				'id' => array(),
				'style' => array(),
			);
			$allowed_html = array(
				'a' => array(
					'class' => array(),
					'id' => array(),
					'style' => array(),
					'href' => array(),
					'rel' => array(),
					'target' => array(),
				),
				'b' => $base_attrs,
				'strong' => $base_attrs,
				'em' => $base_attrs,
				'span' => $base_attrs,
				'ul' => $base_attrs,
				'li' => $base_attrs,
				'ol' => $base_attrs,
			);
		}

		/**
		 * Allow developers to filter what are the allowed HTML on the Excerpt
		 *
		 * @var array Must be compatible to wp_kses structure
		 *
		 * @link https://codex.wordpress.org/Function_Reference/wp_kses
		 */
		$allowed_html = apply_filters( 'tribe_events_excerpt_allowed_html', $allowed_html, $post );

		/**
		 * Allow shortcodes to be Applied on the Excerpt or not
		 *
		 * @var bool
		 */
		$allow_shortcodes = apply_filters( 'tribe_events_excerpt_allow_shortcode', false );

		/**
		 * Filter to stop removal of shortcode markup in the Excerpt
		 * This will remove all text that resembles a shortcode [shortcode 5]
		 *
		 * @var bool
		 */
		$remove_shortcodes = apply_filters( 'tribe_events_excerpt_shortcode_removal', true );

		// Get the Excerpt or content based on what is available
		$excerpt = has_excerpt( $post->ID ) ? $post->post_excerpt : $post->post_content;

		// If shortcode filter is enabled let's process them
		if ( $allow_shortcodes ) {
			$excerpt = do_shortcode( $excerpt );
		}

		// Remove all shortcode Content before removing HTML
		if ( $remove_shortcodes ) {
			$excerpt = preg_replace( '#\[.+\]#U', '', $excerpt );
		}

		// Remove "all" HTML based on what is allowed
		$excerpt = wp_kses( $excerpt, $allowed_html );

		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;
		}

		if ( ! $skip_postdata_manipulation ) {
			// Setup post data to be able to use WP template tags
			setup_postdata( $post );
		}

		/**
		 * Filter the event excerpt used in various views.
		 *
		 * @param string  $excerpt
		 * @param WP_Post $post
		 */
		$excerpt = apply_filters( 'tribe_events_get_the_excerpt', wpautop( $excerpt ), $post );

		if ( ! $skip_postdata_manipulation ) {
			wp_reset_postdata();
		}

		return $excerpt;
	}