WordPress.org

Ready to get started?Download WordPress

Forums

oEmbed and youtube autoplay (3 posts)

  1. hedgehogOo
    Member
    Posted 6 months ago #

    Hello,
    In a website with 3000+ articles, I pull a youtube video url (youtube.com/watch?v=XXXXX) from a custom field, then into the template file I filter it through the_content: <?php echo apply_filters('the_content', *customfield*); ?> so it can be automatically oEmbedded.

    Now my client wants to autoplay those videos in all the articles and I'm having troubles doing it.

    Manually appending ?autoplay=true to the video url appears to be not supported by oEmbed: the url will not be embedded.

    I also tried to code the embed iframe into my template file: <iframe src="*customfield*" frameborder="0" allowfullscreen></iframe> but it does not work because it requires the url to be youtube.com/embed/XXXXX whereas all my custom field data is in the youtube.com/watch?v=XXXXX format.

    So my only solution is to manually edit all 3000+ articles to transform the "normal" youtube urls into the embedded ones?

    Am I missing something here? Maybe some oEmbed filter that I don't know how to use?

    Thank you for your time

  2. Timothy Jacobs
    Member
    Posted 6 months ago #

    I don't know of anyway, but what you could do is something like this.

    $string = "http://www.youtube.com/watch?v=XXXXX";
    $url = parse_url( $string );
    $query_args = $url['query'];
    $query_args = parse_str( $query_args );
    
    $new_string = "http://www.youtube.com/embed/" . $query_args[0];
  3. hedgehogOo
    Member
    Posted 6 months ago #

    Well that's quite unorthodox but it works! I didn't think about it!
    I was a bit confused about the parse_str line cause it didn't work at first, so I did this:

    $string = "http://www.youtube.com/watch?v=XXXXX";
    $url = parse_url( $string );
    $query_args = $url['query']; // returns v=XXXXX
    parse_str( $query_args ); // generates $v with XXXXX value
    $new_string = "http://www.youtube.com/embed/" . $v . "?feature=oembed&autoplay=1";

    Thank you very much sir!!

Reply

You must log in to post.

About this Topic