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('' . $my_tour_video . '');
}
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.
Building a fake embed shortcode and then calling run_shortcode to do this makes no real sense. Try this instead:
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.