I just wrote this function to resize video elements for my WordPress Lightbox Plugin. This specific script is designed to be used with an ajax call. Like this:
Let me know if you have any problems. I just rewrote most of this on the fly, so there may be errors. …but hopefully not. :)
jQuery / Javascript
Add this to your plugin’s javascript file between the
|
1 2 3 4 5 6 7 8 9 10 11 12 |
jQuery(function($){ $('#video-code').bind('paste', function(){ var field = $(this); setInterval(function(){ var vall = $(field).val(); var html = encodeURIComponent(val); $.post(ajaxurl, 'action=clp_resize_video&html='+html, function(response){ $(field).val(response); }); }, 150); }); }); |
PHP WordPress Function
This can be added to your plugin’s functions file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
add_action('wp_ajax_clp_size_video', 'clp_size_video'); function clp_size_video(){ $new_width = 320; $video = stripslashes($_POST['html']); preg_match("/width=\"([^\"]*)\"/i",$video,$w); $w = (integer)$w[1]; preg_match("/height=\"([^\"]*)\"/i",$video,$h); $h = (integer)$h[1]; if(!$new_width) $new_width = $w; $w2 = $new_width; $ratio = (float)($w2/$w); $h2 = (integer)($h * $ratio); $video = str_replace("width=\"$w\"","width=\"$w2\"",$video); $video = str_replace("height=\"$h\"","height=\"$h2\"",$video); echo $video; die(); } |




