$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.


  • if i change the echo contents to words like "testing" it works perfectly. what is it about iframes?! Dec 23 '12 at 4:47

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>',
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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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