SWFObject 2.1

This article is for plugin authors which uses the great swfobject JavaScript library. The new version 2.1 contains a complete new way to emded and prepare the script code for the flash content. It’s NOT backward compatible with the implementation of version 1.5 , so it breaks my plugins wordTube and NextGEN Gallery. Due to the fact that I need to rework this for both plugins, I created a small PHP class which makes it much easier to implement the script code.

For example : 

$swf = new swfobject( $url, 'id', $width, $height, '7', 'false');

This initiate the swfobject with the url of the flash and the width/height settings and set an id name. This PHP class ensure that the id is not used two times, because it append always a internal counter. 

You can add to the $swf object new parameters, attributes or flashvars :

$swf->add_params('wmode', 'opaque');
$swf->add_flashvars( 'myboolval', $option_a, 'false', 'bool');
$swf->add_flashvars( 'myintval', $option_b);
$swf->add_flashvars( 'mystring, 'abcdef');

Now you can generate the output for your content with the follow function :

$out = $swf->output();

And the generated javascript code can be done with the function:

$js = '';
$js .= $swf->javascript();
$js .= '';

Please note that you can add the JavaScript code to any position in your output, but it’s recommended to add this to your header or footer area. To keep it more flexible it will not automatic add the <script>  wrapper around the code.

This functions produce now the follow HTML output :

<div class="swfobject" id="id_1" style="width:160px;height:120px;">
Get the Flash Player to see the flash content.
</div>


var id_1 = {
    params : {
       wmode : "opaque" },
    flashvars : {
        myboolval : "true",
        myintval : "10",
        mystring : "abcdef" },
    attr : {},
    start : function() {
        swfobject.embedSWF("file.swf", "id_1", "160", "120", "7", false, this.flashvars, this.params , this.attr );
    }
}

id_1.start();

This few lines of code should demonstrate how easy you can create dynamic swfobject content with PHP, without build up the JavaScript code manually. The swfobject script will automatic exchange the <div> element with the flash content.

You can download the class from my current GoogleCode trunk :

Download swfobject.php
PHP class @ Google Code

Tagged , , ,

3 thoughts on “SWFObject 2.1

  1. sainsuper says:

    beautiful script, i have tested and it work great, but if the flashvars name has a dot it down’t work.
    example trying to use viral.recommendation fashvar.

  2. alex.rabe says:

    Escape it then you can use it :

    $swfobject->add_flashvars( ”ltas.mediaid”, $var );

  3. sainsuper says:

    thanks very much alex!

Comments are closed.

%d bloggers like this: