1

I have this function to convert any YouTube URL in any post or page to embed :-

function embed_youtube_an($post_content) {
  global $posts; 
  //preg_match('|http://www.youtube.com/watch?v=([a-zA-Z0-9]+)|', $posts->post_content, $matches);
  preg_match('#http://w?w?w?.?youtube.com/watch\?v=([A-Za-z0-9\-_]+)#s', $posts->post_content, $matches);

 if (!empty($matches[1])) { 
    $post_content = '<object width="415" height="250">';
      $post_content .= '<param name="movie" value="http://www.youtube.com/v/' . $matches[1] . '&hl=en_US&fs=1&"></param>';
      $post_content .= '<param name="allowFullScreen" value="true"></param>';
      $post_content .= '<param name="allowscriptaccess" value="always"></param>';
      $post_content .= '<embed src="http://www.youtube.com/v/' . $matches[1] . '&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="250"></embed>';
      $post_content .= '</object>';
      $post_content = $post_content;

  }
     return $post_content;
}

add_filter('the_content', 'embed_youtube_an');

But when open post , all YouTube URL does not show in embed.

Where are the problem?

  • 3
    It's completely unclear what you are asking (taken the info from your comments on Howdy McGees answer). Please edit your question to explain your goal in detail. If English is a problem, take some friend to assist you with rewording it. Thanks. – kaiser Nov 19 '14 at 19:20
  • 1
    Please consult the help center and read How to Ask to see how things work on this site. Editing answers to reply is not how it works. Thanks. – kaiser Nov 20 '14 at 12:51
  • 1
    Also, please take a tour and learn how this site (and for that matter all SE sites) works. – Pieter Goosen Nov 20 '14 at 13:03
  • OEmbed should take care of convert YouTube links into videos. What happens when you remove your code? codex.wordpress.org/Embeds – Seamus Leahy Nov 23 '14 at 17:35
5

EDIT

After reviewing your edit, you may want to try following These Instructions

To embed a video or another object into a post or page, place its URL into the content area. Make sure the URL is on its own line and not hyperlinked (clickable when viewing the post).


It sounds like you would want to use wp_get_oembed() - The examples look like this:

Retrieve the embed code for a URL from a supported oEmbed provider:

<?php $embed_code = wp_oembed_get('http://www.youtube.com/watch?v=tkEvSjdDfiA'); ?>

Retrieve the embed code for a URL from a supported oEmbed provider - with width argument:

<?php $embed_code = wp_oembed_get('http://www.youtube.com/watch?v=tkEvSjdDfiA', array('width'=>400)); ?>

If that doesn't work you could probably apply 'the_content' filter:

apply_filters( 'the_content', 'http://www.youtube.com/watch?v=tkEvSjdDfiA' );

But that filter is probably overkill for what you're trying to achieve.

| improve this answer | |
  • How can use apply_filters( 'the_content', 'http://www.youtube.com/watch?v=tkEvSjdDfiA' ); ?? – user2933912 Nov 19 '14 at 19:09
  • I have more than youtube link in all posts, How can do that – user2933912 Nov 19 '14 at 19:13
  • @user2933912 How are you collecting your links? How are you echoing out your "content"? Please edit your original question and add this code with a little more detail. – Howdy_McGee Nov 19 '14 at 19:41
  • View my updated answer. – Howdy_McGee Nov 19 '14 at 20:03
  • Ok, But i dont knew the url, and i have more than 2000 post have Youtube url, So what the solution for it – user2933912 Nov 20 '14 at 4:27
0

When you enter the URL, put your editor in TEXT mode. Lots of people don't notice the visual editor converts HTML special characters, and deactivates links.

| improve this answer | |
0

You are making wrong use of the the_content filter.

Why your code is not working

  • You are not returning anything back to the referenced content

  • Even if your condition fails, you should pass the referenced content back, otherwise nothing will be displayed if your condition returns false

  • Your preg_match function is using the wrong subject. Use the referenced content here. (This is just a note, you don't need to call the global $post)

  • Your pattern is also wrong, it does not match any pattern thus failing and returning the content as-is

With the current code that you have, I have gone with parse_url() and parse_str() thanks to this answer. It has to be noted that from your original question that you are using the following format

  • http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=related and
  • http://www.youtube.com/watch?v=C4kxS1ksqtw.

This code only works on the format watch?v=

I have modified your code accordingly:

add_filter('the_content', function ($post_content) 
{ 
    parse_str( parse_url( $post_content, PHP_URL_QUERY ),  $matches);

    if (!empty($matches['v'])) { 
        $post_content = '<object width="415" height="250">';
        $post_content .= '<param name="movie" value="http://www.youtube.com/v/' . $matches['v'] . '&hl=en_US&fs=1&"></param>';
        $post_content .= '<param name="allowFullScreen" value="true"></param>';
        $post_content .= '<param name="allowscriptaccess" value="always"></param>';
        $post_content .= '<embed src="http://www.youtube.com/v/' . $matches['v'] . '&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="250"></embed>';
        $post_content .= '</object>';
        $post_content = $post_content;
    }
    return $post_content;
});

EDIT

As a test, the following code

<?php
$string = 'http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=related';
parse_str( parse_url( $string, PHP_URL_QUERY ),  $matches);
?><pre><?php var_dump($matches); ?></pre><?php

gives the following output when dumped

array(2) {
  ["v"]=>
  string(11) "C4kxS1ksqtw"
  ["feature"]=>
  string(7) "related"
}   
| improve this answer | |
  • i try your code and not work with me – user2933912 Nov 20 '14 at 12:35
  • Before your conditional statement, do a var_dump($matches) and see what is printed and work from there. Also, try removing your condition and check if your code inside your conditional is working as expected – Pieter Goosen Nov 20 '14 at 12:40
  • its something, its still show me youtube link – user2933912 Nov 20 '14 at 12:52
  • See my update. Take note of the exceptions. – Pieter Goosen Nov 23 '14 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.