How to show video in excerpt

There many strange solutions on it. It seems that the authors simply did not understand how and when the shortcode [embed] works. Here is the simple code which can be added to functions.php.

 * Allows to show video in excerpt.
 * @param string $post_excerpt
 * @return string
function get_the_excerpt_filter( $post_excerpt ) {
	$pos = mb_strpos( $post_excerpt, '' );
	if ( false !== $pos ) {
		/** @var WP_Embed $wp_embed */
		$wp_embed     = $GLOBALS['wp_embed'];
		$post_excerpt = $wp_embed->run_shortcode( $post_excerpt );

	return $post_excerpt;

add_filter( 'get_the_excerpt', 'get_the_excerpt_filter' );

The code properly process the shortcode like [embed][/embed] in post excerpt and shows the video.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.