• Took me a bit to track this down, but I guess I should have heeded the ‘it may behave unexpectedly in rare cases’ warning 😉

    It appears that when you add a custom oembed provider using wp_oembed_add_provider(), with the ‘Keep paragraph tags’ option on, TinyMCE will still wrap a custom embed provider URL in paragraph tags, which means WordPress won’t pick it up to do the embed. It works fine when this option is not selected.

    There is (sortof) a way around it, although I’ve only got it working in some cases – it’s to add the following function which sends URLs still wrapped in <p></p> tags to WP’s oembed callback:

    add_filter("the_content", "mytheme_detect_wrapped_embeds", 11); // be sure it runs after wpautop on priority 10
    
    	function mytheme_detect_wrapped_embeds($content){
    
    		$content = preg_replace_callback(
    			'|^\<p\>(\s*)(https?://[^\s"]+)(\s*)\</p\>$|im', // URLs on their own line wrapped with <p></p>
    			array($GLOBALS["wp_embed"], "autoembed_callback"),
    			$content
    		);
    
    		return $content;
    
    	}

    I tracked down where the problem is – in this plugin under \mce\wptadv\plugin.js, the oembeds are manually defined (as they also are in WP core). So, I’m not sure of an easy way around this… but is it possible perhaps to have a way to hook new regexes into this file when a new oembed provider is added?

    https://wordpress.org/plugins/tinymce-advanced/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter tdmalone

    (@tdmalone)

    Ok I’ve worked out this is probably the best way around it at the moment – to replace the TinyMCE Advanced version of the wptadv plugin with my own custom copy:

    add_filter("mce_external_plugins", "mytheme_custom_wptadv", 1000); // must be hooked after 999
    
    function mytheme_custom_wptadv($plugins){
    	if(isset($plugins["wptadv"])){
    		$plugins["wptadv"] = get_template_directory_uri() . "/src/js/wptadv-plugin-modified.js";
    	}
    	return $plugins;
    }

    In wptadv-plugin-modified.js I’ve just copied the default wptadv plugin, but added a new regexp for my custom oembed provider.

    I guess the ultimate would be if this plugin was built dynamically from the oembed providers registered with WordPress, but I realise that would add quite a bit more overhead!

    Plugin Author Andrew Ozz

    (@azaozz)

    I’m not sure of an easy way around this… but is it possible perhaps to have a way to hook new regexes into this file when a new oembed provider is added?

    Can probably fire a custom TinyMCE or jQuery event that will let the array of regexes be “filtered”.

    Tried “translating” the regexes from PHP to JS. It’s too complex / not working well.

    Best fix is for WordPress to support embeddable URLs wrapped in <p>. There is already a ticket about that. Then we can drop all this 🙂

    Thread Starter tdmalone

    (@tdmalone)

    Thanks for the reply Andrew and for giving it a go.

    I’ll wait on the progress of that ticket 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘'Keep paragraph tags' option breaks custom oembed providers’ is closed to new replies.