If you care about this, I don’t need to give you too much background. Longtail’s JW Player is a great HTML5/Flash video player that has been around for years, with tons of plugins and a large user base, and at this point there’s very little you can’t do with it. At least as far as playing audio and video goes.

The guy who coded most of JW Player’s core and plugins has also produced a handy WordPress plugin that lets you integrate JW Player into your WordPress site, and for the most part it works beautifully.

There’s at least one exception: While you have the option to integrate the sharing plugin via the plugin’s settings in WordPress, you have one global field for “embed code” that can’t really accept anything useful. You have to URI escape anything that goes in there (which wouldn’t be so bad if there was a way to URI unescape the output), and there’s no way to add a MEDIAID variable in there so that your embed code can be dynamic (which is a complete dealbreaker for almost every user).

Well, I needed this feature, so I found a workaround. And I didn’t want to go hacking into the plugin code (so that I could update it later, without clobbering my fix), so the way I did it was through a PHP regex in my theme’s functions.php.

I first did two things, only one of which should be necessary: In the plugin configuration, I set my custom embed code to [FIXME], which is what my regex will look for, and replace with something useful.

Second, I edited sharing.xml in the WordPress plugin’s plugins directory, to use the sharing-3 repository, rather than sharing-1. I just prefer version 3’s interface, but I also figured newer was probably better. All I did was change the value of the <repository> field from¬†sharing-1 to sharing-3.

But here’s the fix: In my theme’s functions.php, I added a filter for the_content (set at priority 999 so it would run after the JW Player plugin has already replaced my shortcode with the complete embed code), which finds the MEDIAID, and finds my [FIXME] placeholder, and assembles a good embed code. <embed id=”1″ /> is obviously not good embed code; that’s just for¬†simplicity/illustration. You’ll want to replace that with your custom embed code, and here are some ideas on how to do that cleanly.

Theme functions.php

function swap_embed($match) {
	$good_code = '<embed id=\"'.$match[3].'\" />';
	$replaced = '"'.$good_code.'"'.$match[2].',"mediaid":"'.$match[3].'"';
	return $replaced;
}

function fix_jw_embed($content) {
	$regex = '/\"(\[FIXME\])\"(,.*)?,\"mediaid\":\"(\d{1,})\"/i';
	$content = preg_replace_callback($regex, 'swap_embed', $content);
	return $content;
}

add_filter('the_content', 'fix_jw_embed', 999);

Caveat: This only fixes the players in the post body, and not the one generated for the og:video tag for playable embedding on Facebook. I’ll be figuring that out later, and eventually adding the code to this piece, for anyone that needs it.


Comments

2
  • Rakib

    Rakib Rakib

    Reply Author

    thats cool .. but do you try with youtube videos yet?

    Posted on