Skip to content

Commit 71b86cf

Browse files
committed
HTML API: Reliably parse HTML in get_url_in_content()
Trac ticket: Core-63694 This also decodes the URL whereas the previous code didn’t, so strings like `http://` will be properly decoded as `http://`.
1 parent b020981 commit 71b86cf

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/wp-includes/formatting.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5978,16 +5978,20 @@ function wp_unslash( $value ) {
59785978
*
59795979
* @since 3.6.0
59805980
*
5981-
* @param string $content A string which might contain a URL.
5982-
* @return string|false The found URL.
5981+
* @param string $content A string which might contain an `A` element with a non-empty `href` attribute.
5982+
* @return string|false Database-escaped URL via {@see esc_url()} if found, otherwise `false`.
59835983
*/
59845984
function get_url_in_content( $content ) {
59855985
if ( empty( $content ) ) {
59865986
return false;
59875987
}
59885988

5989-
if ( preg_match( '/<a\s[^>]*?href=([\'"])(.+?)\1/is', $content, $matches ) ) {
5990-
return sanitize_url( $matches[2] );
5989+
$processor = new WP_HTML_Tag_Processor( $content );
5990+
while ( $processor->next_tag( 'A' ) ) {
5991+
$href = $processor->get_attribute( 'href' );
5992+
if ( is_string( $href ) && ! empty( $href ) ) {
5993+
return sanitize_url( $href );
5994+
}
59915995
}
59925996

59935997
return false;

0 commit comments

Comments
 (0)