Skip to content
This repository has been archived by the owner on Aug 24, 2018. It is now read-only.

Embeds in new text widget #141

Open
samikeijonen opened this issue May 6, 2017 · 2 comments
Open

Embeds in new text widget #141

samikeijonen opened this issue May 6, 2017 · 2 comments

Comments

@samikeijonen
Copy link

Should embeds work in new text widget in the same way as in post?

For example youtube link in it's own line would show the video.

@westonruter
Copy link
Contributor

Ideally yes, but we cannot currently process embeds in the content due to embeds requiring a post context due to the need to cache in postmeta. If we do support embeds we'd have to limit to those which are handled internally, not any that requiring external requests (and this caching).

@westonruter
Copy link
Contributor

A proof of concept to allow embeds:

diff --git a/wp-includes/widgets/class-wp-widget-visual-text.php b/wp-includes/widgets/class-wp-widget-visual-text.php
index 8509aea..077b57a 100644
--- a/wp-includes/widgets/class-wp-widget-visual-text.php
+++ b/wp-includes/widgets/class-wp-widget-visual-text.php
@@ -64,6 +64,22 @@ class WP_Widget_Visual_Text extends WP_Widget_Text {
 		if ( isset( $instance['filter'] ) ) {
 			if ( 'content' === $instance['filter'] ) {
 
+				add_filter( 'embed_oembed_discover', '__return_false', 100 );
+
+				/**
+				 * @var WP_Embed $wp_embed
+				 */
+				global $wp_embed;
+
+				$using_cache = $wp_embed->usecache;
+				$wp_embed->usecache = false;
+
+				// Hack to get the [embed] shortcode to run before wpautop()
+				add_filter( 'widget_text_content', array( $wp_embed, 'run_shortcode' ), 8 );
+
+				// Attempts to embed all URLs in a post
+				add_filter( 'widget_text_content', array( $wp_embed, 'autoembed' ), 8 );
+
 				/**
 				 * Filters the content of the Text widget to apply changes expected from the visual (TinyMCE) editor.
 				 *
@@ -75,7 +91,12 @@ class WP_Widget_Visual_Text extends WP_Widget_Text {
 				 * @param array          $instance    Array of settings for the current widget.
 				 * @param WP_Widget_Text $this        Current Text widget instance.
 				 */
-				$text = apply_filters( 'widget_text_content', $widget_text, $instance, $this );
+				$text = apply_filters( 'widget_text_content', $text, $instance, $this );
+
+				remove_filter( 'widget_text_content', array( $wp_embed, 'autoembed' ), 8 );
+				remove_filter( 'widget_text_content', array( $wp_embed, 'run_shortcode' ), 8 );
+				remove_filter( 'embed_oembed_discover', '__return_false', 100 );
+				$wp_embed->usecache = $using_cache;
 
 			} elseif ( $instance['filter'] ) {
 				$text = wpautop( $text ); // Back-compat for instances prior to 4.8.

Note that discovery and caching are disabled since postmeta caching cannot be relied upon.

If you try the patch you'll immediately notice a couple things:

  1. A YouTube video is embedded according to $content_width which is clearly wrong in the context of a widget sidebar (usually). Some strategies would be needed for getting the width and height to be correct, per Handle height/width for videos #93.
  2. TinyMCE isn't recognizing the embeds.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants