Tweet Blockquote in Gutenberg

For more information, see “Tweet this” blockquote in Gutenberg block editor.

/**
 * Tweet Quote
 *
 */
function ea_tweet_quote( $block_content, $block ) {
	if( 'core/quote' === $block['blockName'] && !empty( $block['attrs']['className'] ) && 'is-style-tweet' === $block['attrs']['className'] ) {

		$content = explode( '<cite>', $block_content );
		$text = rawurlencode( wp_strip_all_tags( $content[0]) ) . ' ' . get_permalink();
		$url = 'https://twitter.com/intent/tweet?&text=' . $text;
		$seo_data = get_option( 'wpseo_social' );
		if( !empty( $seo_data['twitter_site'] ) )
			$url .= '&via=' . esc_attr( $seo_data['twitter_site'] );

		$original = '</blockquote>';
		$new = '<a class="wp-block-button__link small" href="' . esc_url_raw( $url ) . '" target="_blank" rel="noopener noreferer">Tweet This</a>' . $original;
		$block_content = str_replace( $original, $new, $block_content );
	}
	return $block_content;
}
add_filter( 'render_block', 'ea_tweet_quote', 10, 2 );
Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk