Reputation: 40157
The function below parses through the content passed from the filter and adds rel="nofollow" to all external links it finds and currently skips over all internal links.
Assuming that I have a folder path defined in a variable, say...
$my_folder = "http://mysite.com/recommends/";
How would I augment the function so that it also ads the nofollow to links that match this pattern as well? These will likely be internal links, and these would be the only internal links I would want to nofollow in this example, so they need to be exceptioned from the internal link regex bits somehow.
add_filter('wp_insert_post_data', 'new_content' );
function new_content($content) {
preg_match_all('~<a.*>~isU',$content["post_content"],$matches);
for ( $i = 0; $i <= sizeof($matches[0]); $i++){
if ( !preg_match( '~nofollow~is',$matches[0][$i])
&& !preg_match( '~'.get_bloginfo('url').'~',$matches[0][$i]) ){
$result = trim($matches[0][$i],">");
$result .= ' rel="nofollow">';
$content["post_content"] = str_replace($matches[0][$i], $result, $content["post_content"]);
}
}
return $content;
}
PS: Thanks to Backie from WSX for the current function code...
Upvotes: 0
Views: 1130
Reputation: 226
I have no way to try this out since I'm not a WP user, but assuming your inner loop defines $matches[0][$i]
as the current url to compare:
add_filter('wp_insert_post_data', 'new_content' );
function new_content($content) {
$my_folder = "http://mysite.com/recommends/";
preg_match_all('~<a.*>~isU',$content["post_content"],$matches);
for ( $i = 0; $i <= sizeof($matches[0]); $i++){
if ( !preg_match( '~nofollow~is',$matches[0][$i])
&& (preg_match('~' . $my_folder . '~', $matches[0][$i])
|| !preg_match( '~'.get_bloginfo('url').'~',$matches[0][$i]))){
$result = trim($matches[0][$i],">");
$result .= ' rel="nofollow">';
$content["post_content"] = str_replace($matches[0][$i], $result, $content["post_content"]);
}
}
return $content;
}
Edit: Might have to add a preg_quote around $my_folder, I'm just a bit confused since it's not done for get_bloginfo('url')
Upvotes: 1