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.

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

share|improve this question

2 Answers 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' );
share|improve this answer
    
Hi, sorry for the delay. –  RogerNobrega Mar 4 '13 at 0:34

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.

share|improve this answer
    
I was first, just ;) –  bungeshea 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? –  bungeshea 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

 
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.