0

I'm trying to make this shortcode work but without any success. All attempts gives me a white square.

  function youtube($atts) {
    extract(shortcode_atts(array(
    "value" => 'https://www.youtube.com/watch?v=EhWopzlRwZ8',
    "width" => '475',
    "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("youtube", "youtube");
1
  • And what is the problem with it? What do you mean by "white square"? Commented Dec 16, 2013 at 13:21

1 Answer 1

3

Few remarks about your code:

1) First you should use something like:

https://www.youtube.com/v/EhWopzlRwZ8

instead of the YouTube page:

https://www.youtube.com/watch?v=EhWopzlRwZ8

in your embed code.

2) You can always use the [embed] shortcode if you need to embed YouTube videos:

[embed width="600" height="400"]https://www.youtube.com/watch?v=EhWopzlRwZ8[/embed]

3) You should consider adding a prefix to your shortcode callback, for example macko_tarana_ to avoid function name collision with other plugins.

4) Try to avoid extract(..., use $atts = shortcode_atts(... instead.

5) Consider atting the third parameter to shortcode_atts, i.e. the shortcode name so you can use the shortcode_atts_{$shortcode} filter. More about it here in the Codex.

6) Always escape the input attributes, for example with esc_attr() or esc_url().

Hope this helps.

2
  • +1 for point #5. I so get fed up by this that I edited Codex and left a note about it.
    – kaiser
    Commented Dec 16, 2013 at 13:36
  • thanks @kaiser. I agree and usually the third attribute is missing when you really need it ;-)
    – birgire
    Commented Dec 16, 2013 at 13:40

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.