Web Development Blog

Insert Flash Media Embed Code TinyMCE Plugin

03.22.2010 03:19PM by Evan Johnson

I’ve run into a problem providing my clients good, easy-to-use media embed functionality with the existing JavaScript WYSIWYG editors. The big players, CKEditor (formery FCKEditor) and TinyMCE both provide a "Flash" button which works okay with YouTube and other large video providers, but what about embedding a Picasa slideshow? None of the WYSIWYG editors come with plugin to just insert some HTML code. If you want to embed something like a Picasa slideshow, customized YouTube player, or Google Map you have open up the source code, find the location you want to insert it, and paste in the code. Fine for some, but difficult and scary for most as it breaks the WYSIWYG paradigm.

On one of my latest Drupal websites there are multiple bloggers, none of whom are particularly tech savvy. I set up the CMS with TinyMCE because I like how they can drag and drop images around (I just wish Drupal had an image embed uploader like WordPress!). After a while they complained that the Insert Flash button in TinyMCE just wasn’t cutting it. So I set about fixing it. How hard could it be to make a plugin that just inserts some HTML code where your cursor is? Like it already does for images?

It turns out it is very easy. I was able to just edit the TinyMCE "Example" plug-in, which already did basically exactly what I wanted. So here I provide it to you, Internet. I hope it is useful, and please let me know if it’s broken (or could work better). Thanks!

UPDATED: (9-17-2010) I fixed some bugs with IE. It should work better cross-browse now.

Download the plugin:

I found some tips on installing TinyCME plugins here: Installing Plugin Newbie Question
Here is the official guide to creating a plugin for TinyMCE 3: TinyMCE:Create plugin/3.x

To use this plugin, install it in the TinyMCE plugin folder and just include it in the TinyMCE.init() call like you would the other plugins. Example ("embed" is the name of the plugin):


<script type="text/javascript" src="<your installation path>/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
  tinyMCE.init({
    theme : "advanced",
    mode : "textareas",
    plugins : "embed, fullscreen, emotions, preview",
    theme_advanced_buttons3_add : "embed, fullscreen, emotions, preview"
  });
</script>

Disclaimer: I am not a TinyMCE guru, and not even that hot at JavaScript. I probably cannot help you with other TinyMCE plugins, or even this one. I only got this to work because the Example plugin was so close to what I already needed. But thanks for stopping by!

Comment

Gravatar Image
On May 18, 05:50 AM, (Link)
Dr.Death said:

after add "embed" in plugins options, tiny stop init without errors :(

Gravatar Image
On May 24, 09:13 AM, (Link)
DeMoN3 said:

Great! Thanks a lot!

Gravatar Image
On Sep 11, 03:54 PM, (Link)
Arman said:

In IE8 it’s give an error

Gravatar Image
On Sep 17, 12:28 PM, (Link)
Chris said:

Thanks for this plugin!

Here are some quick steps I had to take in order to install this module using WYSIWYG+TinyMCE in Drupal 6.

1. Upload the plugin folder to sites/all/libraries/tinymce/jscripts/tiny_mce/plugins/embed .
2. Open sites/all/modules/wysiwyg/editors/tinymce.inc.
3. Inside of the wysiwyg_tinymce_plugins() function, there is an array called $plugins. Add the following code to the array:

'embed' => array( 'path' => $editor['library path'] .'/plugins/embed', 'buttons' => array('embed' => t('Embed'), 'embed' => t('Embed')), 'url' => 'http://www.chilipepperdesign.com/2010/03/22/insert-flash-media-embed-code-tinymce-plugin', 'internal' => TRUE, 'load' => TRUE,
),

Hope that helps!

Gravatar Image
On Sep 17, 02:16 PM, (Link)
Evan Johnson said:

Yo! I fixed the IE bugs. Thanks to everyone who alerted me to them and helped me fix it. The download link has been updated with the new version. Cheers!

Gravatar Image
On Nov 4, 06:38 AM, (Link)
Connie said:

as the first comment points out, TinyMCE stopps without any error message

that is a pity as I need this plugin …

(I used it in CMSMS with TinyMCE 3.3.8 )

Gravatar Image
On Nov 4, 09:29 AM, (Link)
Evan Johnson said:

@Connie – Everything seemed to work fine when I tested the new IE fix a couple of months ago. Please post a fix if you figure out the problem! The code is open-source, and I don’t have time to work on it right now.

Gravatar Image
On Nov 5, 03:47 AM, (Link)
Connie said:

Evan,

I am not a programmer, unfortunately, I am a webdeveloper who has to include client’s whishes…

So I will not be able to assist you

my problem for sure has nothing to do with IE, as I NEVER use this browser ;=)

I use Firefox 3.6.11 on WINDOWS XP at this time

Cheers, Connie

Gravatar Image
On Dec 7, 11:51 AM, (Link)
George said:

Thank you so much! Fixed my problem in a few short minutes!

Gravatar Image
On Jan 27, 01:35 PM, (Link)
David Waldorf said:

Just curious if you’ve seen any issues related to the new format of YouTube’s embed code, and whether you’ve ever tried using it in a setup where htmLawed is also used

Gravatar Image
On Feb 24, 11:26 AM, (Link)
ChatCadet said:

Thanks for the fix.

Gravatar Image
On Mar 30, 12:20 PM, (Link)
Andrew said:

I just copied Chris’s instructions for getting WYSIWYG/TinyMCE to use the embed plugin but WYSIWYG clears the embedded content before it saves the page or perhaps it never really embeds it…

It looks like a Youtube video is embedded however if you inspect the HTML, it is not embedded and if you save, it is not part of the content.

Any ideas?

Gravatar Image
On Mar 30, 01:59 PM, (Link)
Andrew said:

Here’s the solution:
1) After making Chris’s patch to tinymce.inc I added another patch just below it that I found on a Drupal discussion.

'iframe' => array( 'buttons' => array('iframe' => t('Iframe')), 'extended_valid_elements' => array('iframe[src|width|height|name|align|frameborder]'), ),

2) Then I went into the TinyMCE profile in WYSIWYG and activated 'iframe' (I had already activated 'embed' via Chris’s instructions).

This makes TinyMCE editor allow an iFrame inside the editor because otherwise the editor window would just ignore the iFrame code.

Link to discussion that provided the answer: http://drupal.org/node/69524#comment-4163564

Gravatar Image
On Mar 30, 02:59 PM, (Link)
Evan Johnson said:

@Andrew – Thanks for following up!

Gravatar Image
On Mar 30, 07:59 PM, (Link)
Andrew said:

No worries Evan,
To avoid issues with WYSIWYG module updates wiping out the patch, I wrapped it up into a module for those of us using Drupal 6 with WYSIWYG and TinyMCE.

All you have to do is:
1) activate the module,
2) put the embed plugin into sites/all/libraries/tinymce/jscripts/tiny_mce/plugins/embed
3) use the WYSIWYG interface to tick the checkboxes for iframe and embed.

I’m just thinking about where it should be uploaded to for downloading. Do you want to host it?

I still have to write the readme.txt instructions but I’ve installed it on a couple of sites to check it out and it works just fine.

Andrew

Gravatar Image
On Apr 5, 12:04 AM, (Link)
Andrew said:

Evan,
If anyone wants to download the module I created that acts as a bridge between your embed plugin and WYSIWYG/TinyMCE within Drupal (D6 for now), they can get it here: http://bitsnbytes.lunadesign.org/blog/drupal-6-embedding-youtube-clips-tinymce-wysiwyg

The post is a little long winded for those who have followed your discussion so just get the module and read the enclosed readme file for instructions on how to use it.

Cheers,
Andrew

  Subscribe to article comments