| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: [iframe] |
| 4 |
Plugin URI: http://web-profile.com.ua/wordpress/plugins/iframe/ |
| 5 |
Description: [iframe src="http://player.vimeo.com/video/3261363" width="100%" height="480"] shortcode |
| 6 |
Version: 2.2 |
| 7 |
Author: webvitaly |
| 8 |
Author Email: webvitaly(at)gmail.com |
| 9 |
Author URI: http://web-profile.com.ua/wordpress/ |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( !function_exists( 'iframe_embed_shortcode' ) ) : |
| 13 |
|
| 14 |
function iframe_enqueue_script() { |
| 15 |
wp_enqueue_script( 'jquery' ); |
| 16 |
} |
| 17 |
add_action('wp_enqueue_scripts', 'iframe_enqueue_script'); |
| 18 |
|
| 19 |
function iframe_embed_shortcode($atts, $content = null) { |
| 20 |
$defaults = array( |
| 21 |
'width' => '100%', |
| 22 |
'height' => '480', |
| 23 |
'scrolling' => 'no', |
| 24 |
'class' => 'iframe-class', |
| 25 |
'frameborder' => '0' |
| 26 |
); |
| 27 |
// add defaults |
| 28 |
foreach ($defaults as $default => $value) { |
| 29 |
if (!array_key_exists($default, $atts)) { |
| 30 |
$atts[$default] = $value; |
| 31 |
} |
| 32 |
} |
| 33 |
// special case maps |
| 34 |
$src_cut = substr($atts["src"], 0, 35); |
| 35 |
if(strpos($src_cut, 'maps.google' )){ |
| 36 |
$atts["src"] .= '&output=embed'; |
| 37 |
} |
| 38 |
$html = ''; |
| 39 |
if( isset( $atts["same_height_as"] ) ){ |
| 40 |
$same_height_as = $atts["same_height_as"]; |
| 41 |
}else{ |
| 42 |
$same_height_as = ''; |
| 43 |
} |
| 44 |
|
| 45 |
if( $same_height_as != '' ){ |
| 46 |
$atts["same_height_as"] = ''; |
| 47 |
if( $same_height_as != 'content' ){ // we are setting the height of the iframe like as target element |
| 48 |
if( $same_height_as == 'document' || $same_height_as == 'window' ){ // remove quotes for window or document selectors |
| 49 |
$target_selector = $same_height_as; |
| 50 |
}else{ |
| 51 |
$target_selector = '"' . $same_height_as . '"'; |
| 52 |
} |
| 53 |
$html .= ' |
| 54 |
<script> |
| 55 |
jQuery(document).ready(function($) { |
| 56 |
var target_height = $(' . $target_selector . ').height(); |
| 57 |
$("iframe.' . $atts["class"] . '").height(target_height); |
| 58 |
//alert(target_height); |
| 59 |
}); |
| 60 |
</script> |
| 61 |
'; |
| 62 |
}else{ // set the actual height of the iframe (show all content of the iframe without scroll) |
| 63 |
$html .= ' |
| 64 |
<script> |
| 65 |
jQuery(document).ready(function($) { |
| 66 |
$("iframe.' . $atts["class"] . '").bind("load", function() { |
| 67 |
var embed_height = $(this).contents().find("body").height(); |
| 68 |
$(this).height(embed_height); |
| 69 |
}); |
| 70 |
}); |
| 71 |
</script> |
| 72 |
'; |
| 73 |
} |
| 74 |
} |
| 75 |
$html .= "\n".'<!-- Iframe plugin v.2.2 (wordpress.org/extend/plugins/iframe/) -->'."\n"; |
| 76 |
$html .= '<iframe'; |
| 77 |
foreach ($atts as $attr => $value) { |
| 78 |
if( $attr != 'same_height_as' ){ // remove some attributes |
| 79 |
if( $value != '' ) { // adding all attributes |
| 80 |
$html .= ' ' . $attr . '="' . $value . '"'; |
| 81 |
} else { // adding empty attributes |
| 82 |
$html .= ' ' . $attr; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
$html .= '></iframe>'; |
| 87 |
return $html; |
| 88 |
} |
| 89 |
add_shortcode('iframe', 'iframe_embed_shortcode'); |
| 90 |
|
| 91 |
endif; |
| 92 |
|