Skip to content

Instantly share code, notes, and snippets.

@magemonkey
Last active December 18, 2015 11:59
Show Gist options
  • Save magemonkey/5779761 to your computer and use it in GitHub Desktop.
Save magemonkey/5779761 to your computer and use it in GitHub Desktop.
Simple and quick way to make links for a domain nofollow in your posts & pages content if, like for me, all of the nofollow plugins failed at the task or broke your site.
<?php
/**
* Allows you to create nofollow links easily in WP content for specific urls and
*
* File: nofollow.php
* User: marvinroman
* Created: 6/14/13
*
* Change the items in [] as well as the subdomain array
* If you don't know the subdomains run the file once with the array empty and it will show you all the found subdomains without making any changes
* Place this file in your root WP and then visit http://wpdomain.com/nofollow.php
* Probably best to chmod 000 and run from terminal like /usr/local/bin/php ./nofollow.php
*/
// url to mark nofollow
$url = "[nofollowdomain.com]";
// list of subdomains of url above to mark nofollow
$subDomains = array(
'www',
'forum',
'information',
'faq',
'resources',
''
); // best to include '' as well for base domain
// sites base url
$self = "[blog.domain.com]";
$disableRootSelf = true;// whether to also ban link to root self ie http://www.self.com/
include_once('wp-config.php'); //needs to be in root so it has access to wp-config where DB info is held
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$findSQL = "SELECT `ID`, `post_content` FROM `" . DB_NAME . "`.`wp_posts` WHERE `post_content` LIKE '%$url%' AND `post_type` NOT LIKE 'revision';";
$result = mysql_query($findSQL, $link);
$foundSubDomains = array();
$ids = array();
while ($row = mysql_fetch_array($result)) {
$content = $row[1];
if ($disableRootSelf && strpos($content, $self) > 0):
$escapedURL = str_replace(array('/', '.'), array('\/', '\.'), $self);
$preREGEX = '';
$postREGEX = '\/?';
$findREGEX = '/<a\s+(t[a-z]+="[^"]+")?\s+href="http:\/\/(' . $preREGEX . $escapedURL . ')(' . $postREGEX . ')"/';
$content = preg_replace($findREGEX, '<a rel="nofollow" href="http://$2" $1', $content);
endif;
$escapedURL = str_replace(array('/', '.'), array('\/', '\.'), $url);
$preREGEX = sprintf('(%s)\.?', implode('|', $subDomains));
$postREGEX = '[^"]*';
$findREGEX = '/<a[\s]+(t[a-z]+="[^"]+")?[\s]*href="http:\/\/(' . $preREGEX . $escapedURL . ')(' . $postREGEX . ')"/';
$content = preg_replace($findREGEX, '<a rel="nofollow" href="http://$2" $1', $content);
$findSQL = '/href="http:\/\/([a-z]+)\.' . $escapedURL . '/';
if (preg_match_all($findSQL, $content, $matches) >= 1) {
foreach ($matches[1] as $sub) {
array_push($foundSubDomains, $sub);
}
$foundSubDomains = array_unique($foundSubDomains);
}
if (!is_null($content)) {
$replaced = mysql_real_escape_string($content, $link);
$replaceSQL = "UPDATE `" . DB_NAME . "`.`wp_posts` SET `post_content` = '" . $content . "' WHERE `ID` = '" . $row[0] . "';";
if (count($subDomains) > 0) mysql_query($replaceSQL, $link);
array_push($ids, $row[0]);
}
}
echo "\nList of No Follow Subdomains for $url\n";
echo implode(",\n", $subDomains);
echo "\n\nList of Found Subdomains for $url \n";
echo implode(",\n", $foundSubDomains);
echo "\n\nPosts affected on $self\n";
echo count($ids) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment