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'm using Wordpress 3.5 which natively supports embeds into posts and pages from many sites, YouTube included.

When I embed a Youtube video directly onto a post or a page (by simply copying and pasting the video url), it works fine and no cross-domain policy errors are thrown in Chrome or Firefox.

However, when I add the URL from a function inside a custom page template (outside the main loop), that I built for a custom post type, it throws an error.

Here is what my function looks like:

function my_tour_video()
{
    global $post,
           $wp_embed; 

    $my_tour_video = get_post_meta($post->ID, "_my_tour_video", true);  
    echo $wp_embed->run_shortcode('[embed]' . $my_tour_video . '[/embed]');
}

In my custom field I have tried the "embed" youtube url and also just the "watch" url copied and pasted from the browser.

You can try this sample url: http://www.youtube.com/embed/Y7AvqD2loX4

Error in Chrome looks something like:

Unsafe JavaScript attempt to access frame with URL http://www.mydomain.com from frame with URL http://www.youtube.com/embed/Y7AvqD2loX4. Domains, protocols and ports must match.

Any suggestions on how to do this the right way would be very helpful at this point.

share|improve this question
add comment

1 Answer

Building a fake embed shortcode and then calling run_shortcode to do this makes no real sense. Try this instead:

echo $wp_embed->shortcode(array(), $my_tour_video);

Edit: Additionally, when I use your code on a test page, using that embed, I get no such error as you're describing. If you can come up with a reproducible test case, then it might be solvable.

If you are using the /embed/ URL, then that might cause it. Use the real URL of the video instead, as it shows when you're looking at YouTube's video page. The same URL you would normally paste into a post's content.

share|improve this answer
    
Otto, your code doesn't work. When I try echo $wp_embed->run_shortcode(array(), $my_tour_video) it echos the url only. My code works but I'm still getting the cross-domain error. –  IntricatePixels Mar 13 '13 at 20:39
    
Look again. I said "->shortcode", not "->run_shortcode". Although this probably won't fix your cross-domain error, but it will make a lot more sense for generating the embedded video. –  Otto Mar 13 '13 at 21:14
    
Your error doesn't make any sense anyway, there is no actual difference in processing the shortcode yourself in a template vs. letting it be processed in the content. Either both throw the error, or neither does. In any case, that error is at the HTML and Javascript level, and has nothing whatsoever to do with the PHP code. –  Otto Mar 13 '13 at 21:20
    
Additionally, when I use your code on a test page, using that embed, I get no such error as you're describing. If you can come up with a reproducible test case, then it might be solvable. Edit: Ahh, you might be using the /embed/ URL. That could cause it. Use the real URL of the video instead, as it shows when you're looking at YouTube's video page. The same URL you would normally paste into a post's content. –  Otto Mar 13 '13 at 21:21
    
Hi Otto - can you edit your answer so I can vote you up :) –  IntricatePixels Mar 14 '13 at 19:33
show 1 more comment

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.