Last active
June 11, 2021 08:59
-
-
Save vovadocent/478f7f38b8d83faf9f6fbb74817a9aa8 to your computer and use it in GitHub Desktop.
Remove rel="nofollow" attribute from internal links into comment text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Filter | |
* Remove rel="nofollow" attribute from internal links into comment text | |
*/ | |
function vd_comment_text( $comment_text, $comment, $args ) { | |
// find all <a> tags | |
$regex = '#<\s*?a\b[^>]*>.*?</a\b[^>]*>#s'; | |
preg_match_all( $regex, $comment_text, $matches ); | |
foreach ( $matches as $atag ) { | |
// find rel="nofollow" | |
$nof_regex = '/rel\=[\"\']nofollow.*[\"\']\S*?/i'; | |
preg_match( $nof_regex, $comment_text, $nof_matches ); | |
if ( empty( $nof_matches[ 0 ] ) ) | |
continue; | |
// find link domain | |
$href_regex = '/[https]+\:\/\/([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3})\/\S*?/i'; | |
preg_match( $href_regex, $comment_text, $href_matches ); | |
if ( empty( $href_matches[ 1 ] ) ) | |
continue; | |
// check if the domain is th current domain | |
if( strpos( site_url(), $href_matches[ 1 ] ) !== false ){ | |
$comment_text = str_replace( $nof_matches[0], '', $comment_text ); | |
} | |
} | |
return $comment_text; | |
} | |
add_filter( 'comment_text', 'vd_comment_text', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment