0

I wanna make a simple shortcode that returns the youtube link in a specified size and use the content. Something like:

[yt]http://youtu.be/Nl29v5pfxTw[/yt]

I have this code, but I want to simplify it:

    function youtube($atts) {
        extract(shortcode_atts(array(
                "value" => 'http://',
                "width" => '620',
                "height" => '350',
                "name"=> 'movie',
                "allowFullScreen" => 'true',
                "allowScriptAccess"=>'always',
        ), $atts));
        return '<object style="height: '.$height.'px; width: '.$width.'px"><param name="'.$name.'" value="'.$value.'"><param name="allowFullScreen" value="'.$allowFullScreen.'"></param><param name="allowScriptAccess" value="'.$allowScriptAccess.'"></param><embed src="'.$value.'" type="application/x-shockwave-flash" allowfullscreen="'.$allowFullScreen.'" allowScriptAccess="'.$allowScriptAccess.'" width="'.$width.'" height="'.$height.'"></embed></object>';
}
add_shortcode("yt", "youtube");

The problem is that with this code I need to use [yt value="http://youtu.be/Nl29v5pfxTw"] and I want to just use the [yt]$content[/yt]

How I can achieve that?

Thanks

2 Answers 2

2

Shortcode functions accept a second parameter which contains the value in between the shortcode opening and closing tags:

function youtube( $atts, $value = 'http://' ) {
    extract( shortcode_atts( array(
        "width" => '620',
        "height" => '350',
        "name"=> 'movie',
        "allowFullScreen" => 'true',
        "allowScriptAccess"=>'always',
    ), $atts ) );
    return '<object style="height: '.$height.'px; width: '.$width.'px"><param name="'.$name.'" value="'.$value.'"><param name="allowFullScreen" value="'.$allowFullScreen.'"></param><param name="allowScriptAccess" value="'.$allowScriptAccess.'"></param><embed src="'.$value.'" type="application/x-shockwave-flash" allowfullscreen="'.$allowFullScreen.'" allowScriptAccess="'.$allowScriptAccess.'" width="'.$width.'" height="'.$height.'"></embed></object>';
}
add_shortcode( 'yt', 'youtube' );
1
  • Hi, sorry for the delay. Commented Mar 4, 2013 at 0:34
0

Your callback function needs to accept a second parameter.

function youtube($atts, $content) {
        extract(shortcode_atts(array(
                "value" => 'http://',
                "width" => '620',
                "height" => '350',
                "name"=> 'movie',
                "allowFullScreen" => 'true',
                "allowScriptAccess"=>'always',
        ), $atts));
        return '<object style="height: '.$height.'px; width: '.$width.'px"><param name="'.$name.'" value="'.$content.'"><param name="allowFullScreen" value="'.$allowFullScreen.'"></param><param name="allowScriptAccess" value="'.$allowScriptAccess.'"></param><embed src="'.$content.'" type="application/x-shockwave-flash" allowfullscreen="'.$allowFullScreen.'" allowScriptAccess="'.$allowScriptAccess.'" width="'.$width.'" height="'.$height.'"></embed></object>';
}

I believe that is right. There are examples in the Codex about how to do this.

5
  • I was first, just ;)
    – shea
    Commented Jan 8, 2013 at 0:44
  • Yes, you were. Didn't notice until I hit 'submit'. I up-voted you though :)
    – s_ha_dum
    Commented Jan 8, 2013 at 1:13
  • For some reason none of these worked. Commented Jan 8, 2013 at 17:58
  • @RogerNobrega Please elaborate - what didn't work?
    – shea
    Commented Jan 17, 2013 at 2:04
  • Hi, sorry fot the delay. It didnt worked, it shows a blank square only Commented Mar 4, 2013 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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