Extracting URLs from text in WordPress with wp_extract_urls

The wp_extract_urls function in WordPress is a function that extracts URLs from a given text. This function scans through the provided text, identifies all the URLs it contains, and returns them as an array. It can extract both absolute and relative URLs.

This function can be employed in various scenarios where URL extraction from text is required. For instance, it can be used in content analysis, where it helps in identifying all the external and internal links present in the post content. Similarly, it can be used in SEO-related plugins to identify links for further processing.

The wp_extract_urls function is not limited to extracting URLs from post content, but can also be used to extract URLs from any string input. This makes it a versatile function for various applications where URL extraction is needed.

Parameters Accepted by the wp_extract_urls Function

The wp_extract_urls function in WordPress is designed to accept a single parameter. This parameter is detailed below:

  • $content (string) – This is a mandatory parameter. It refers to the content from which the URLs will be extracted.

Return Value of the wp_extract_urls Function

Upon execution, the wp_extract_urls function returns an array of strings. Each string in this array represents a URL that has been successfully extracted from the content passed to the function.

If the function does not receive any parameters, it will not be able to extract any URLs and will thus return an empty array.


How to Extract URLs from a Given Text

$text = "Check out my website at https://www.example.com and my blog at https://blog.example.com!";
$urls = wp_extract_urls( $text );
print_r( $urls );

This code snippet extracts all URLs found in the given text string. The wp_extract_urls function takes the text as an argument and returns an array of URLs. The print_r function is then used to print this array, showing all the URLs extracted from the text.

How to Display Extracted URLs in a Post Content

$post_content = get_the_content();
$urls = wp_extract_urls( $post_content );

if ( ! empty( $urls ) ) {
 foreach ( $urls as $url ) {
 echo "<p>$url</p>";

This code snippet extracts all URLs from the content of a WordPress post. The get_the_content function gets the content of the current post in the loop, and the wp_extract_urls function is used to extract all URLs from this content. If the array of URLs is not empty, each URL is displayed in its own paragraph.

How to Count the Number of URLs in a Text

$text = "Visit us at https://www.example.com. Follow us on Twitter at https://twitter.com/example and Facebook at https://facebook.com/example.";
$urls = wp_extract_urls( $text );
$count = count( $urls );
echo "<p>The text contains $count URLs.</p>";

This code snippet counts the number of URLs in a given text string. The wp_extract_urls function is used to extract all URLs from the text, and the count function is then used to count the number of elements in the returned array. The number of URLs is then displayed in a paragraph.


The wp_extract_urls function in WordPress serves as a handy tool for extracting URLs from a text or string. It works by scanning through the input string and identifying all the URLs embedded within it. This function is particularly useful in situations where developers need to isolate and manipulate the URLs within a given text, such as in the case of SEO optimization, data analysis, or when creating certain types of plugins or widgets that interact with URLs.

Related WordPress Functions