What can be an efficient way to determine videos that may be embedded in a post?

If the embed code is directly added to a post, it is easy to determine using regular expressions but some plugins embed videos through shortcodes or in custom fields. In these cases, the actual embed code is added at the time of rendering the post.

  • Are you looking to find all the videos in a post no matter how they are included? What specific plugins are you talking about? – hornj Dec 30 '13 at 20:10
  • Let's say I am only looking for YouTube URLs in a post. – Amit Agarwal Dec 30 '13 at 21:26

This is the statement I am currently using to extract YouTube URLs from a post:

preg_match('|http://www.youtube.com/watch\?v=([a-zA-Z0-9\-\_]+)|', $post->post_content, $matches);

$matches[1] contains the ID of the first video. This may not cover all the YouTube URL formats, but it should give you a start.

| improve this answer | |
  • This works only when the YouTube URL is included in the blog posts. Some plugin insert videos through short codes, custom fields and am looking to extract such URLs that aren't int he body. – Amit Agarwal Jan 1 '14 at 11:15
  • Right, I thought that is what you wanted. But do you know exactly what methods you are using? – hornj Jan 1 '14 at 13:49

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.