wp_rel_nofollow_callback

Callback to add rel=nofollow string to HTML A element.

Description

(string) wp_rel_nofollow_callback( (array) $matches ); 

Will remove already existing rel="nofollow" and rel='nofollow' from the string to prevent from invalidating (X)HTML.

Returns (string)

HTML A Element with rel nofollow.

Parameters (1)

0. $matches (array)
The matches.

Usage

  1. if ( !function_exists( 'wp_rel_nofollow_callback' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The matches. 
  5. $matches = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_rel_nofollow_callback($matches); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function wp_rel_nofollow_callback( $matches ) { 
  2. $text = $matches[1]; 
  3. $atts = shortcode_parse_atts( $matches[1] ); 
  4. $rel = 'nofollow'; 
  5.  
  6. if ( preg_match( '%href=["\'](' . preg_quote( set_url_scheme( home_url(), 'http' ) ) . ')%i', $text ) || 
  7. preg_match( '%href=["\'](' . preg_quote( set_url_scheme( home_url(), 'https' ) ) . ')%i', $text ) 
  8. ) { 
  9. return "<a $text>"; 
  10.  
  11. if ( ! empty( $atts['rel'] ) ) { 
  12. $parts = array_map( 'trim', explode( ' ', $atts['rel'] ) ); 
  13. if ( false === array_search( 'nofollow', $parts ) ) { 
  14. $parts[] = 'nofollow'; 
  15. $rel = implode( ' ', $parts ); 
  16. unset( $atts['rel'] ); 
  17.  
  18. $html = ''; 
  19. foreach ( $atts as $name => $value ) { 
  20. $html .= "{$name}=\"$value\" "; 
  21. $text = trim( $html ); 
  22. return "<a $text rel=\"$rel\">";