Black Friday & Cyber Monday sale, save 30%! Valid until November 30th at 11:59:59 PM (UTC - 6). Enter BFCM2015 at checkout or Click here to apply discount →

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

The source of the function looks like this:

function wp_extract_urls( $content ) {
	$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:

$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. 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 => '' )
  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];

Error: Please enter a valid email address

Error: Invalid email

Error: Please enter your first name

Error: Please enter your last name

Error: Please enter a username

Error: Please enter a password

Error: Please confirm your password

Error: Password and password confirmation do not match