PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

YouTube Video URL converted to YouTube Embed Code

  1. Hello support forum,

    I want my users to be able to input a URL to a YouTube video, then get only the ID, so that I can embed it how I want into a post (autoplay, size, HD).

    For example user enters:
    http://www.youtube.com/watch?v=ALEUp3kXvvk

    Then I want to take that and only use the ID of the video which is ALEUp3kXvvk and insert it into my custom embed:

    <iframe src="http://www.youtube.com/embed/ALEUp3kXvvk?rel=0&autoplay=1&showinfo=0&controls=0&HD=1&modestbranding=1" frameborder="0" width="560" height="315"></iframe>

    Any help or direction would be much appreciated.

    Thanks!

    Posted 11 years ago on Tuesday July 31, 2012 | Permalink
  2. This is not related at all to Gravity Forms, but I've done this before so I can point you in the right direction. See this StackExchange post:

    http://stackoverflow.com/questions/3392993/php-regex-to-get-youtube-video-id

    Specifically:

    [php]
    $url = "http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=relate";
    parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
    echo $my_array_of_vars['v'];
      // Output: C4kxS1ksqtw

    That parse_str will work to return the video ID. You will have to change this code example to get $url from your form field, and you likely won't echo $my_array_of_vars['v'] but will use that in your embed code.

    Hope that helps.

    Posted 11 years ago on Tuesday July 31, 2012 | Permalink
  3. Thanks, I was approaching this problem from Gravity Forms doing the work.

    However, using this solution I made another plugin (the plugin that is displaying the video) use this code to pass the video (that Gravity forms got) to my own custom embed in PHP.

    Thanks for the help.

    Posted 11 years ago on Wednesday August 1, 2012 | Permalink
  4. Thank you for the update.

    Posted 11 years ago on Wednesday August 1, 2012 | Permalink

This topic has been resolved and has been closed to new replies.