WordPress 3.7 introduced a new function called wp_extract_urls(). This is a handy function that allows you to extract URLs from text strings. Simply pass the text string to the function and it will return an array of URLs.

At the time of writing this, wp_extract_urls() is not documented on the Codex, but Rarst has included it on QueryPosts.com.

The source of the function looks like this:

1
2
3
4
5
6
7
8
9
10
11
function wp_extract_urls( $content ) {
	preg_match_all(
		"#((?:[\w-]+://?|[\w\d]+[.])[^\s()<>]+[.](?:\([\w\d]+\)|(?:[^`!()\[\]{};:'\".,<>?«»“”‘’\s]|(?:[:]\d+)?/?)+))#",
		$content,
		$post_links
	);
 
	$post_links = array_unique( array_map( 'html_entity_decode', $post_links[0] ) );
 
	return array_values( $post_links );
}

Here’s a quick example of how you can use it:

1
2
3
4
5
$string = 'Risus porttitor aliquet nec mid augue penatibus? Pellentesque, vel scelerisque sociis ridiculus porta elementum platea! Platea dictumst vel risus? Vel etiam? Tincidunt, pid porttitor penatibus amet porta nec odio diam, massa et tempor. Ultricies ultricies cras, sociis urna nec mid ac est augue dignissim? Odio, aliquet nec sed sit lacus, lundium risus pellentesque velit velit penatibus, duis magna, sed auctor. Scelerisque purus odio tempor rhoncus amet velit parturient turpis, pid, sagittis, eu? Vel et nascetur placerat habitasse adipiscing augue penatibus, sed natoque habitasse. Amet, dolor phasellus pulvinar vel dictumst porttitor, eu tincidunt porttitor pulvinar? Urna. http://google.com is a cool site. Porttitor, magna elementum quis lundium et platea? Rhoncus enim? Diam! Ultrices? Integer rhoncus eros, mus ridiculus eu enim porta magna mauris. Nec, in eu dictumst.';
 
$urls = wp_extract_urls( $string );
// will return an array like this:
// array( 0 => 'http://google.com' )
  1. Rami Y

    Nice one!

    I will add documented to the Codex in a few days.

  2. G. M.

    Worth noting any query string in urls is stripped out.

  3. Shea Bunge

    Would be useful when working with link post formats:

    $link = wp_extract_urls( $content )[0];

Comments are closed.