Extracts and returns the first URL from passed content.
Parameters
$contentstringrequired- A string which might contain an
Aelement with a non-emptyhrefattribute.
Source
function get_url_in_content( $content ) {
if ( empty( $content ) ) {
return false;
}
$processor = new WP_HTML_Tag_Processor( $content );
while ( $processor->next_tag( 'A' ) ) {
$href = $processor->get_attribute( 'href' );
if ( is_string( $href ) && '' !== $href ) {
return sanitize_url( $href );
}
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 3.6.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.