get_url_in_content() WordPress Function

This function allows you to get the URL of the first link in the post content.

get_url_in_content( string $content ) #

Extracts and returns the first URL from passed content.


Parameters

$content

(string)(Required)A string which might contain a URL.


Top ↑

Return

(string|false) The found URL.


Top ↑

Source

File: wp-includes/formatting.php

function get_url_in_content( $content ) {
	if ( empty( $content ) ) {
		return false;
	}

	if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) {
		return esc_url_raw( $matches[2] );
	}

	return false;
}


Top ↑

Changelog

Changelog
VersionDescription
3.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More