wp_targeted_link_rel_callback
函数
wp_targeted_link_rel_callback ( $matches )
- 参数
-
-
(array)
$matches
Single match.- Required: 是
-
(array)
- 返回值
-
- (string) HTML A Element with `rel=”noopener”` in addition to any existing values.
- 定义位置
-
-
wp-includes/formatting.php
, line 3267
-
wp-includes/formatting.php
- 引入
- 5.1.0
- 弃用
- –
Callback to add `rel=”noopener”` string to HTML A element.
Will not duplicate an existing ‘noopener’ value to avoid invalidating the HTML.
function wp_targeted_link_rel_callback( $matches ) { $link_html = $matches[1]; $original_link_html = $link_html; // Consider the HTML escaped if there are no unescaped quotes. $is_escaped = ! preg_match( '/(^|[^\])['"]/', $link_html ); if ( $is_escaped ) { // Replace only the quotes so that they are parsable by wp_kses_hair(), leave the rest as is. $link_html = preg_replace( '/\(['"])/', '$1', $link_html ); } $atts = wp_kses_hair( $link_html, wp_allowed_protocols() ); /** * Filters the rel values that are added to links with `target` attribute. * * @since 5.1.0 * * @param string $rel The rel values. * @param string $link_html The matched content of the link tag including all HTML attributes. */ $rel = apply_filters( 'wp_targeted_link_rel', 'noopener', $link_html ); // Return early if no rel values to be added or if no actual target attribute. if ( ! $rel || ! isset( $atts['target'] ) ) { return ""; } if ( isset( $atts['rel'] ) ) { $all_parts = preg_split( '/s/', "{$atts['rel']['value']} $rel", -1, PREG_SPLIT_NO_EMPTY ); $rel = implode( ' ', array_unique( $all_parts ) ); } $atts['rel']['whole'] = 'rel="' . esc_attr( $rel ) . '"'; $link_html = implode( ' ', array_column( $atts, 'whole' ) ); if ( $is_escaped ) { $link_html = preg_replace( '/['"]/', '\$0', $link_html ); } return ""; }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。