Use WordPress excerpt as subtitle in Instant Articles

Unfortunatelly, Instant Articles for WP doesn’t add WordPress excerpt as subtitle to Facebook Instant Articles.
Please see my solution to fix this.
I changed function to_instant_article() (thanks to FewKinG) and function get_the_subtitle() in the plugin’s file class-instant-articles-post.php:

function-to_instant_article()

if ( $this->has_subtitle() ) {
			$header->withSubTitle ( $this->get_the_subtitle() ) ;
		}

public-function-get_the_subtitle()

public function get_the_subtitle() {
		// If we have already been through this function, we’ll have the result stored here
		if ( ! is_null( $this->_subtitle ) ) {
			return $this->_subtitle;
		}
        
        $post = get_post( $this->get_the_id() );
		// This should ideally not happen, but it may do so if someone tampers with the query.
		// Returning the same protected post excerpt as "usual" may help them identify what’s going on.
		if ( post_password_required( $this->get_the_ID() ) ) {
			return __( 'There is no excerpt because this is a protected post.' );
		}
		// Make sure no “read more” link is added.
		add_filter( 'excerpt_more', '__return_empty_string', 999 );
		/**
		 * Apply the default WP Filters for the post excerpt.
		 *
		 * @since 0.1
		 *
		 * @param string  $post_excerpt  The post excerpt.
		 */
		$subtitle = apply_filters( 'get_the_excerpt', $post->post_excerpt );
        
		/**
		 * Filter the subtitle for use in instant articles
		 *
		 * @since 0.2
		 *
		 * @param string                 $subtitle              The current subtitle for the post.
		 * @param Instant_Article_Post   $instant_article_post  The instant article post
		 */
		$subtitle = apply_filters( 'instant_articles_subtitle', $subtitle, $this );
		$this->_subtitle = $subtitle;
		return $subtitle;
	}

You can download the class-instant-articles-post.php from Github.

Leave a Reply