WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i'm using the default "insert video" function from wordpress (which inserts a normal hyperlink to the video only) and would like to replace that link with something else like a video player.

my question: what's the regex pattern for grabbing all links inside a post? thx

share|improve this question
Why not just add this into the other question? wordpress.stackexchange.com/questions/12806/… – t31os Mar 23 '11 at 11:46
up vote 5 down vote accepted

I'll answer your question below, but have you looked at using embeds? Look here for more information: http://codex.wordpress.org/Embeds

The simplest regex for this would look something like http\:\/\/.*\b

Here's an example of it in action:


$file = 'test.txt';

$fp = fopen($file, 'r');

$contents = fread($fp, filesize($file));

$matches = array();

preg_match_all('/http\:\/\/.*\b/', $contents, $matches);



Where the file I reference looks like this:




and the return looks like this:

    [0] => Array
            [0] => http://wordpress.stackexchange.com/questions/12809/retrieving-all-links-from-a-post
            [1] => http://www.youtube.com
            [2] => http://ca3.php.net/manual/en/function.preg-match.php

share|improve this answer
I'm not sure why my code is red here. It syntax highlighted correctly in the edit view. – Gavin Anderegg Mar 23 '11 at 12:38
@Lea Cohen: Thanks! Sadly, that didn't solve it :( – Gavin Anderegg Mar 23 '11 at 12:59
The [regex] tag for the question forced regex-style syntax highlighting on the answers too, instead of the default style which works for PHP. You can now give a language hint, and I edited your question to do it (partly because this was the first time I could try this, and I was curious). – Jan Fabry Mar 23 '11 at 13:22
@Jan Fabry: Awesome! Thank you! – Gavin Anderegg Mar 23 '11 at 13:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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