Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
$width = 560;
$height = 315;
$code = "I8M_uCnxhwY";

$homepage = "/";
$currentpage = $_SERVER['REQUEST_URI'];
if($homepage!=$currentpage) {
echo '<iframe width=".$width." height=".$height." src="http://www.youtube.com/embed/'.$code.'" frameborder="0" allowfullscreen></iframe>';
}  

So, that's my current code. I'm trying to display a YouTube embedded video in an iframe within a PHP script that shows said iFrame only if it is not on the home page. (i.e. is a blog post)

If anyone could assist me that would be great.

It seems to acknowledge something should be there, but doesn't show it. I get no errors.

Thanks!

share|improve this question
    
if i change the echo contents to words like "testing" it works perfectly. what is it about iframes?! –  damondicenzo Dec 23 '12 at 4:47

2 Answers 2

up vote 2 down vote accepted

I would say that's a quirk with the plugin. In this case, it's better to make a shortcode.

Add this code to your functions.php file:

function wpse_77036_embed_video( $atts ) {
    extract( shortcode_atts( array(
        'homepage' => '/',
        'currentpage' => $_SERVER['REQUEST_URI'],
        'width' => 560,
        'height' => 315,
        'code' => 'I8M_uCnxhwY',
    ), $atts ) );

    if ( $homepage != $currentpage ) {
        return sprintf(
            '<iframe width="%s" height="%s" src="http://www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>',
            $width,
            $height,
            $code
        );
    }  
}
add_shortcode( 'embed-video', 'wpse_77036_embed_video' );

You can then use this in your post like:

[embed-video width="560" height="315" code="I8M_uCnxhwY"]

If no width, height or code is supplied, it will use the defaults as defined in the function. You can also pass a custom homepage or currentpage to the shortcode in the same way you pass the width and height. If you need any more help, please post a comment below.

share|improve this answer
    
brilliant! works perfect! thanks! –  damondicenzo Dec 23 '12 at 10:58

If you have more requirements with iframe, not specific for the YouTube video, then is it also possible, that you allow iframe inside the TinyMCE. See my answer in a older question.

Make WordPress WYSIWYG not strip out iframe's

But for easy to use the video is the shortcode a nice solution.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.