1

I've made a custom meta box for adding an URL to the post, which can be displayed later on the frontend page. This url is inserted in my database correctly. Then I'm getting the url in my code by:

<?php
 $get_video_url = get_post_meta(get_the_ID(), 'video_url', true);
 //echo $get_video_url;
 //When I echo the variable, it's giving me te correct URL back for each post
?>

Now I'm trying to get my iFrame to work. But it gives me this error in console:
Refused to display 'https://www.youtube.com/watch?v=OVoXDk8ps10' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

What I know is that I have watch?v= inside the URL when it's get out of the database and for an iFrame the URL needs to be with embed like: https://www.youtube.com/embed/OVoXDk8ps10

Question
What my question is, is how I'm able to change the URL after I get it from the database and be able to get the iFrame to work on the frontend?

I'm using this code trying to displaying my iFrame:

<p style="text-align: center;"><iframe src="<?php echo $get_video_url; ?>" 
frameborder="0" allowfullscreen></iframe></p>
5
  • I'm not gonna post this as an answer because it does not actually answer your specific question but it's easier to use WP_Embed. Just get the global wp_embed first by putting glbal $wp_embed; anywhere above your video location (preferably on the top of the page) and then put echo $wp_embed->run_shortcode( '[embed]' . $get_video_url . '[/embed]' );
    – Jebble
    Commented Apr 21, 2017 at 9:54
  • Already fixed the problem, thanks for you comment and sorry for wasting you time.. @JeffreyvonGrumbkow
    – Stan
    Commented Apr 21, 2017 at 10:04
  • I'd still advice to use the integrated embed functionality but glad you got a solution :) BTW glbal is a typo, use global $wp_embed;
    – Jebble
    Commented Apr 21, 2017 at 10:06
  • Aha, I will take a look at it. Thanks! @JeffreyvonGrumbkow
    – Stan
    Commented Apr 21, 2017 at 10:08
  • 1
    @JeffreyvonGrumbkow It is working like a charm! Using your suggestion now :)
    – Stan
    Commented Apr 21, 2017 at 10:15

1 Answer 1

2

Almost every time when I post a question, I'm able to find the solution on the internet.. Sorry guys and girls for the spam!

Solution

<div class="featured-img-blog">

 <?php
  global $wp_embed;
  $get_video_url = get_post_meta(get_the_ID(), 'video_url', true);

  echo "<p align=center>" . $wp_embed->run_shortcode('[embed]' . $get_video_url . '[/embed]') . "</p>";
 ?>                               
</div>

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.