0

I have 2 WP_Editors, 1 is the main editor I use for content and the 2nd is an editor I use for the user to insert videos and any extra content describing the video. My question is, is there an easy way to pull the video URL before (during or after) oEmbed grabs it? I've tried to use a preg_match but feel that there's too many variations of the youtube URL to find efficiently.

Here's what I've tried:

$videoContent = get_post_meta($post->ID, 'videoContent', true);
$search = '#(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12})#x';
$url = preg_match($search, $videoContent);
print_r($url);

I've tried to search around but can't seem to enter the correct keywords to pull up anything useful, only 'how to embed' and such.

1

Maybe you should just add a custom field to accept the url itself, in stead of the wp_editor textarea; then append the video embed after you work with the input.

Let me know if you need any help setting that up.

OR:

You could add a separete textarea to accept just comma separated or line-break separated urls (Because you stated below that you might need mulitple video embeds)

Here is some code to use while saving that will clean up the textarea input:

if ( isset( $_REQUEST['your_url_list'] ) ):


    $input = $_REQUEST['your_url_list'];

    $data = preg_split("/[\r\n,]+/", $input, -1, PREG_SPLIT_NO_EMPTY);
    $option_list = array();
    foreach( $data as $d )
        $option_list[] =  trim( $d );

    update_post_meta( $post_id, 'your_url_list', maybe_serialize( $option_list ) );

endif;
  • 1
    Agree. I just wrote an answer that saves a youtube URL and then displays the video with oembed, so that might help: stackoverflow.com/a/22986053/383847 – helgatheviking Apr 11 '14 at 19:37
  • I agree, I've done that in the past except in this situation my content is displayed in Tabs, one of them being videos where the user can add as many videos as they'd like along with text describing the video (video intro text if you will) and title headers etc. which is why I decided to go the wp_editor() route. I just need to pull one of the videos (if my video meta isn't empty) and display it in the archive as a quick link. – Howdy_McGee Apr 11 '14 at 19:42
0

As your problem is to find YouTube URL from HTML contents, following link may help you: https://stackoverflow.com/a/5831191/147618

Here is required regex copied from the same link.

// Linkify youtube URLs which are not already links.
function linkifyYouTubeURLs($text) {
    $text = preg_replace('~
        # Match non-linked youtube URL in the wild. (Rev:20130823)
        https?://         # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)? # Optional subdomain.
        (?:               # Group host alternatives.
          youtu\.be/      # Either youtu.be,
        | youtube         # or youtube.com or
          (?:-nocookie)?  # youtube-nocookie.com
          \.com           # followed by
          \S*             # Allow anything up to VIDEO_ID,
          [^\w\s-]       # but char before ID is non-ID char.
        )                 # End host alternatives.
        ([\w-]{11})      # $1: VIDEO_ID is exactly 11 chars.
        (?=[^\w-]|$)     # Assert next char is non-ID or EOS.
        (?!               # Assert URL is not pre-linked.
          [?=&+%\w.-]*    # Allow URL (query) remainder.
          (?:             # Group pre-linked alternatives.
            [\'"][^<>]*>  # Either inside a start tag,
          | </a>          # or inside <a> element text contents.
          )               # End recognized pre-linked alts.
        )                 # End negative lookahead assertion.
        [?=&+%\w.-]*        # Consume any URL (query) remainder.
        ~ix', 
        '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
        $text);
    return $text;
}

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.