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

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' );
  • Hi, sorry for the delay. – RogerNobrega Mar 4 '13 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.

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

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.