I’m making a theme where some posts have an associated video, from either Vimeo or YouTube. At the moment, users can add this to a post by adding a video ID into one of two custom fields, called (surprisingly enough), 'YouTube' and 'Vimeo'. My theme then takes this info and adds it to the standard embed code for these sites, and outputs it on the post page.

However, I’d like to make things easier for the users of the site, if possible. I’d like to replace these two fields with a single custom field, and also allow them to enter the full URL of the video on Vimeo or YouTube (instead of the ID).

I presume that this would require two things: firstly, that the URL is recognised as coming from either Vimeo or YouTube, and secondly, that the rest of the URL is stripped, so that I can use the ID to output the embedded video nice and clean on post pages.

I know that there are plenty of plugins that do this kind of thing (and other similar things) automatically, but I’d prefer to have this built-in, if at all possible.

I’m not a PHP wizard so I haven’t ventured into doing anything about it yet, but I’m hoping that this could be made to happen via some functions.php magic.

Anyone have any ideas about the best way to proceed?


1 Answer 1


Whipped up a nifty little parsing function for you:

function wpse44048_parse_video_link( $link ) {
    $url = 'some url'; // pass this in
    $parsed_url = parse_url( $url );

    $host = $parsed_url['host'];

    // check that the service exists, otherwise return false
    if( strpos( $host, 'youtube.com' ) !== false
     && strpos( $host, 'youtu.be' ) !== false
     && strpos( $host, 'vimeo.com' ) !== false ) {
         return false;

    // set $service
    if( strpos( $host, 'youtube.com' ) !== false
     || strpos( $host, 'youtu.be' ) !== false ) {
        $service = 'youtube';
    if( strpos( $host, 'vimeo.com' ) !== false ) {
        // handle vimeo
        $service = 'vimeo';

    // set $video_id
    if( strpos( $host, 'youtube.com' ) !== false ) {
        // handle youtube regular url
        $vars = array();

        parse_str( $parsed_url['query'], $vars );

        $video_id = $vars['v'];
    if( strpos( $host, 'youtu.be' ) !== false ) {
        // handle youtube shortened URL
        $video_id = $parsed_url['path'];
    if( strpos( $host, 'vimeo.com' ) !== false ) {
        // handle vimeo
        $video_id = $parsed_url['path'];

    return array(
        'service' => $service, // youtube or vimeo
        'id'      => $video_id // the id of the video

No testing done on that, but I based it on this stackoverflow answer. The function is expansible, though you may want to rewrite it. I wrote it to use a centralized value for the services so youtube.com and youtu.be links wouldn't return a different service if you changed them, but you could definitely do existence check, set $service, and set the $video_id all in one if, elseif, else group. General concept is there for you though.

  • Hi m0r7if3r, thanks for that. Like I said, I’m a PHP klutz, so I’m not sure I really understand what you’ve given me here, but I’ll go try it out and report back. Thanks again! :)
    – Tom
    Mar 2, 2012 at 21:29
  • Which part of it is unclear?
    – mor7ifer
    Mar 3, 2012 at 2:25
  • Hi m0r7if3r. The problem is not that any of it is unclear. The problem is that I am a front-end designer, mainly working with CSS. So, for me, PHP can get pretty mysterious pretty fast. If I put your script in functions.php, how do I then get it to output the ID in single.php? Thanks again.
    – Tom
    Mar 5, 2012 at 14:16
  • 2
    What you really should be doing is going through php basics from something like w3schools. You'll never be able to understand how to implement it if your fundamental understanding of PHP is such that my last post was too complex for you.
    – mor7ifer
    Mar 7, 2012 at 18:02
  • 2
    @Tom - contrary to popular belief, it doesn't take six months to learn to read PHP. Please take m0r7if3r's advice and go to w3schools, and read through the "PHP Basics" section. It'll take you a few hours, and you'll save hundreds more by not scratching your head over basic syntax. Seriously, it will give you the right vocabulary to ask what you need to ask, and give you a basis for understanding and implementing the answers you get. 2-3 hours, max - not 6 months. If you won't do it for our sanity, do it for your own!
    – SickHippie
    Mar 7, 2012 at 19:15

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.