Embed YouTube using the WP2 WYSIWYG Editor

Training TinyMCE

Now that I have a hackable editor that isn’t a primary part of WordPress, I can tell it to stop stripping my <video> tags. My installation of tinyMCE has two lines in its main javascript file, tiny_mce.js:

this._def(“valid_elements”,”+a[id|style|rel|rev|charset|hreflang|dir|lang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup],-strong/-b[class|style],-em/-i[class|style],-strike[class|style],-u[class|style],#p[id|style|dir|class|align],-ol[class|style],-ul[class|style],-li[class|style],br,img[id|dir|lang|longdesc|usemap|style|class|src|onmouseover|onmouseout|border|alt=|title|hspace|vspace|width|height|align],-sub[style|class],-sup[style|class],-blockquote[dir|style],-table[border=0|cellspacing|cellpadding|width|height|class|align|summary|style|dir|id|lang|bgcolor|background|bordercolor],-tr[id|lang|dir|class|rowspan|width|height|align|valign|style|bgcolor|background|bordercolor],tbody[id|class],thead[id|class],tfoot[id|class],-td[id|lang|dir|class|colspan|rowspan|width|height|align|valign|style|bgcolor|background|bordercolor|scope],-th[id|lang|dir|class|colspan|rowspan|width|height|align|valign|style|scope],caption[id|lang|dir|class|style],-div[id|dir|class|align|style],-span[style|class|align],-pre[class|align|style],address[class|align|style],-h1[id|style|dir|class|align],-h2[id|style|dir|class|align],-h3[id|style|dir|class|align],-h4[id|style|dir|class|align],-h5[id|style|dir|class|align],-h6[id|style|dir|class|align],hr[class|style],-font[face|size|style|id|class|dir|color],dd[id|class|title|style|dir|lang],dl[id|class|title|style|dir|lang],dt[id|class|title|style|dir|lang]“);

this._def(“extended_valid_elements”,”");

Yeah, that first one’s a doozie. But take a look at it a sec. Valid Elements! This is a complete list of every tag and its allowed modifiers.

Check out the first one. Hey, that’s <a>. and it has ‘href’ listed as an allowed modifier. That means it’s allowing <a xhref=”xxxx” mce_href=”xxxx”></a>! This is the line that tells my suppimental installation of tinyMCE what tags are allowed to proceed from the WYSIWYG editor into the actual post!

This first line is pretty crazy though, I mean look at it. That’s, like, the full XHTML spec for tinyMCE. I don’t want to mess with that one. Apparently, tinyMCE doesn’t want me to monkey with it either, because they’ve made a nice line called ‘extended_valid_elements’ for me to put all the crap they don’t want to be associated with. Since the only thing being stripped from my cool YouTube code are simple <video> tags, I’ll add it here, and tell my installation of tinyMCE to leave them be!

this._def(“extended_valid_elements”,”video”);

note: I spent about an hour to an hour-and-a-half messing with this line in the wrong installation of tinyMCE. Don’t make the changes in the default install of tinyMCE that comes with WordPress! You’re not using that installation! You won’t see the changes! Even if you could, they would be overwritten when the next version of tinyMCE comes with a WordPress upgrade, so don’t do it!

Success!

Now, open up a new post. Mmm, pretty editor. click ‘html’. (you have to enter the code this way, otherwise the editor will correctly be assuming you don’t want it rendered in the page). enter the following code: <video>[youtube permalink]</video>. Close the html editor, going back to tinyMCE. You should just see the permalink here, since the <video> tags are, well, tags.

Save the post, and check it out!

An embedded YouTube video, all done via WYSIWYG with one simple tag:

http://www.youtube.com/v/hBi89-STzsU

(the above video is from this code, entered in the html editor: <video>http://www.youtube.com/v/hBi89-STzsU</video>)

Next step

Now all I need to do is copy the ‘link’ button, but modify it so that instead of taking an URL and adding <a xhref=” + URL + “> + TEXT + </a> to the inputs, it will take my YouTube input and add the <video> tags to either end.

In addition, if I’m not completely sick of dealing with this, I might go back and see which tags of the YouTube-supplied code are being cut, and just add those tags to my exception list, however…

there is a plugin out there that plays multiple types of video (YouTube, Google Video) with a single tag. It’s called ExtremeVideo, and it uses a [gv] tag. You’ll notice those are brackets, and not angle brackets, so I’m not sure if tinyMCE’s exception list will recognize it as being ok, if I tell it to ignore ‘gv’.

I’ll post back if I get it working, because ideally, I do want a single tag I can enter in the editor that will play any video file. I just started with WPvideo because it was easy, and it works.

Pages: 1 2

48 Responses to “Embed YouTube using the WP2 WYSIWYG Editor”

  1. Very nice site, I was an avid reader of your blog before.

  2. Hey thanks!

  3. It took me a few attempts to get youtube working on wordpress…so I just went for a youtube plugin on plaintext and use deepest sender firefox extension if I want to compose picture posts :)

  4. neat. Everyone has their own preferences, if it works, cheers.

  5. If I remember correct, a “media” plugin is on the current todo list for TinyMCE and hopefully should end up on a release in either end of May or sometime in June, hope this solves your problems.

  6. Cool! Thanks for the update, but I’m unable to find any record of such a plugin. I know there are a few out there that automate the code-entering process of embedding video, but the problem is that none of these plugins add the embedding code to the exception list.

    The result is that all of the plugins I sawe make it very easy to enter video using the wysiwyg editor, but the tags get stripped out when you save. What we need is a way for the plugin to hook into extended_valid_elements. Of course, this enables bad people to write plugins that allow for arbitrary code to be executed also.

    Maybe this new plugin will hook extended_valid_elements, and the tinyMCE will require validation before adding anything to extended_valid_elements?

  7. [...] Selbst WordPress kann mit dem Code umgehen, danke Geeklimit.com für die Klasse Erklärung. Powered by WPvideo 0.60 [...]

  8. There is already a hook into MCE in WordPress.

    Make a normal WordPress plugin with a function that uses the “mce_options” action hook. This function needs to do this:
    echo ‘extended_valid_elements : “insertYourElementsHere”,’;

    That’s pretty much all there is to it.

    BTW, the main thing that seems to be preventing you from inserting the normal YouTube stuff is the fact that it has an embed tag. Adding that to the elements array might prove helpful.

  9. Can’t seem to get this to work. I assume I should be working with ‘tiny_mce.js’ in the wp-content/plugins/editormonkey/tinymce folder?

  10. Make sure Editor Monkey is turned on. Options > EditorMonkey in the WP-Admin backend. (Must be logged in as admin)

    also, in wp-content/plugins/editormonkey/tinymce/tiny_mce.js, the there are three element definitions right near the top – search for ‘extended_valid_elements’ (no quotes). It will be empty. Make it look like this:

    (the xhtml spec, leave this alone)
    this._def(“valid_elements”,”+a[id|style|rel|rev|charset|hreflang|dir|lang|tabindex|accesskey|type|name|href|target|title|class|onfocus|onblur|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup],-strong/-b[class|style],-em/-i[class|style],-strike[class|style],-u[class|style],#p[id|style|dir|class|align],-ol[class|style],-ul[class|style],-li[class|style],br,img[id|dir|lang|longdesc|usemap|style|class|src|onmouseover|onmouseout|border|alt=|title|hspace|vspace|width|height|align],-sub[style|class],-sup[style|class],-blockquote[dir|style],-table[border=0|cellspacing|cellpadding|width|height|class|align|summary|style|dir|id|lang|bgcolor|background|bordercolor],-tr[id|lang|dir|class|rowspan|width|height|align|valign|style|bgcolor|background|bordercolor],tbody[id|class],thead[id|class],tfoot[id|class],-td[id|lang|dir|class|colspan|rowspan|width|height|align|valign|style|bgcolor|background|bordercolor|scope],-th[id|lang|dir|class|colspan|rowspan|width|height|align|valign|style|scope],caption[id|lang|dir|class|style],-div[id|dir|class|align|style],-span[style|class|align],-pre[class|align|style],address[class|align|style],-h1[id|style|dir|class|align],-h2[id|style|dir|class|align],-h3[id|style|dir|class|align],-h4[id|style|dir|class|align],-h5[id|style|dir|class|align],-h6[id|style|dir|class|align],hr[class|style],-font[face|size|style|id|class|dir|color],dd[id|class|title|style|dir|lang],dl[id|class|title|style|dir|lang],dt[id|class|title|style|dir|lang]“);

    (custom spec – put the tags to be ignored here)
    this._def(“extended_valid_elements”,“video”);

    (invalid elements spec, leave this alone)
    this._def(“invalid_elements”,”");

    Of course, double-check that you have WP-Video installed and active…it’s the one that makes the

    Any other problems, post back

  11. Thanks for the quick reply – I really want to get this feature working. I didn’t have Wp-Video installed (Ooops). Now that it is installed and active as well as EditorMonkey, this is what happens. I put: http://www.youtube.com/v/ND0xNuCKgKA into my html window, hit update and it turns into: http://www.youtube.com/v/ND0xNuCKgKA

    ‘Powered by WPvideo 0.60′ does show up as an active link on the post, but the url is displayed in text and there is no video box.

    I appreciate your help – this is a great feature I’ve wanted for a while.

  12. Ok let’s try that again! I put ”www.youtube.com/v/ND0xNuCKgKA” (without the single quotes) into my HTML window, hit update and it turns into: ””www.youtube.com/v/ND0xNuCKgKA (without the single quotes).

  13. For the love of God . . . the video tags (this editor keeps striping them out) start on either side of the youtube url then end up together in front of my youtube url after I hit update in the html editor window.

  14. that’s odd. you’re on wp2, right?

  15. Yes, I’m using wp2. Bizarre. Any other thoughts?

  16. no, I don’t have any other thoughts. You could try editing the standard wp TinyMCE js file, and try that one, but make a backup of the js file first (tinymce.jsOLD or something)

    very strange, should be working.

  17. Thanks a ton, man. Helped me finally get the shizzle embedded like I wanted it to be.

  18. no prob!

  19. I tried to add in embed and object into the extended_valid_elements list instead because these are the 2 tags used by youtube. It seems not to be working. Is there anything wrong with what I am doing?

  20. EditorMonkey…

    EditorMonkey is an GUI editor for wordpress. It has 2 GUI editors rolled into one. Though the FCKEditor cannot work, I think the TinyMCE is more than enough suited for my use.
    I have installed this GUI editor a few weeks ago. And today I fin…

  21. Thanks for the sweet tips, I finally got my vid up! I just started my WP http://www.charlesrequina.com/blog/
    Thanks for your help!

  22. [...] About a day I have been searching on the net on how to embed youtube video into wordpress post. Many website I have come across only tell me to disable the visual rich editor but I dislike that method. I want to add it without disable visual rich editor. It is very hard to find it but at last I came across geeklimit site. [...]

  23. This must have taken a while to suss man. Cheers
    I do have a prob however.
    Same as Tod
    the video tags (this editor keeps striping them out) start on either side of the youtube URL then end up together in front of my youtube URL after I hit update in the HTML editor window.

    Any ideas yet?

    Cheers

  24. With new 1.01 version you can add videos using:

    [video]link_to_video[/video]

    This works on the visual rich editor… :)

  25. woo!

    looks like v1.01 is going to help quite a few people.

    English page: http://www.skarcha.com/wp-plugins/wpvideo/#english

  26. [...] Embed YouTube using the WP2 WYSIWYG Editor | GeekLimit (tags: wordpress totry) [...]

  27. I am TOTALLY new to WordPress, so this may well be a stupid beginner question, but I have been trying to get WPVideo version 1.0.2 to work for several hours now, and need to ask someone for help. I use the [video]youtube_url[/video] syntax in the rich editor, and what gets previewed and published is a link to the plug-in authors page (http://www.skarcha.com/wp-plugins/wpvideo/) with the video title and time displayed below it. But no working link to the video. What am I doing wrong? I copied the text into the style’s .CSS file, installed and activated the plug-in right, but no video. Appreciate any thoughts anyone can offer.

  28. My apologies for not testing enough before asking for help. I just got a Google Video to work fine, so my problem is something particular to using YouTube videos. I copied and pasted the URLs for YouTube and Google identically, and one works and one doesn’t. Anybody have any ideas?

  29. the wp2 “rich” text editor sucks.
    even in the “HTML Source Editor”-mode nothing works.

  30. look very nice , thanks to you

  31. Thanx a lot for the wpvideo plugin. It worked like a charm.

  32. Thanks for the update on Viper Quick tags .. very helpful and also thanks for sharing your difficult experience.. learnt a lot from it about wordpress and videos :)

  33. It’s fun. ATP Subscribe to the RCC perhaps

  34. nice work .. i’ve been successful at embedding flash video, vimeo, even lightbox etc into my concrete5 based site vie the MCE editor, the only thing now is when i try and use the shadowbox plugin to put flash videos, the screen stays black. these are the same movies that worked just fine earlier .. can anyone offer any advice? thanks!

  35. Hm that sounds good but I would like to know more details.

  36. Your news is a cool stuff man, keep it going.

  37. That’s good man, keep it going.

  38. Good information to me.

  39. This is a very good stuff man. But you can be more specific next time. See ya !

  40. This is defenitely one of the key post on this blog. Keep it going man !

  41. Hey, maybe this is a bit offf topic but in any case, I have been surfing about your blog and it looks really neat. impassioned about your writing. I am creating a new blog and hard-pressed to make it appear great, and supply excellent articles. I have discovered a lot on your site and I look forward to additional updates and will be back.

  42. Great story and example

  43. This is a very nice article, keep it going mate !

  44. Well, the article is really the freshest on that notable topic. I agree with your conclusions and also can thirstily look forward to your approaching updates. Saying thanks will not simply be acceptable, for the huge clarity in your writing. I definitely will promptly grab your rss feed to stay privy of any updates. Pleasant work and much success in your business efforts!

  45. Sylvia Bertalan Says:
    January 16, 2011 at 6:29 am

    Really nice work

Leave a Reply