Does such Wordpress plugin exist?

LilMosey

Elite Member
Jr. VIP
Joined
Apr 8, 2016
Messages
2,724
Reaction score
2,214
Hi all, i would like to know if anyone know such Wordpress plugin (see picture below).

I own a site with a lot of links linking to different websites, so what i would like to get is something like "Outlink protection".

I'm googling, but can't find such plugin for Wordpress. Any idea?

bNudCXb

Thanks :)
 
Thanks for this, however, is this just a popup warning? I'm looking for something which will take my visitors to mydomain.com/redirect/link?http://OutLinkSite.com/article/ and they would get redirected automatically within 5seconds.
You don't really need a plugin for this. You could simply modify the content before it is shown in the page. As for example, you could try pasting the following to your functions.php (modify it to your own scenario pls).

Code:
/* Warning: Use AND/OR modify at your own risk. */

function getRootDomain($url) {
    $urlParts = parse_url( $url );
    if ( $urlParts && isset( $urlParts['host'] ) ) {
        return $urlParts['host'];
    }
    return false;
}


function rewriteExternal($html) {
        $domain = str_replace('.', '\.', getRootDomain(get_site_url()));
        //var_dump($domain);
        // The url for your redirection script
        $prefix = 'http://www.example.com?goto=';

        // a regular expression to determine if
        // this link is within your site, edit for your
        // domain or other needs
        $is_internal = '/(?:^\/|^\.\.\/)|'.$domain.'/';

    $dom = new DOMDocument();

    // Parse the HTML into a DOM
    $dom->loadHTML($html);

    $links = $dom->getElementsByTagName('a');

    foreach ($links as $link) {
            $href = $link->getAttribute('href');
            if (!preg_match($is_internal, $href)) {
                $link->getAttributeNode('href')->value = $prefix . $href;
                $link->setAttributeNode(new DOMAttr('rel', 'nofollow'));
                $link->setAttributeNode(new DOMAttr('target', '_blank'));
            }
    }

    // returns the updated HTML or false if there was an error
    return $dom->saveHTML();
}

function outboundLinksModifier(  $content  ) {
    if ( is_main_query() /* && is_singular( 'specific_post_type_or_types' ) */) {
        return  rewriteExternal($content) ;
    }
}
add_action( 'the_content', 'outboundLinksModifier', 10, 2 );

This code will replace all the external links with your chosen redirect URL. Always try keeping things simple. ;)
 
You don't really need a plugin for this. You could simply modify the content before it is shown in the page. As for example, you could try pasting the following to your functions.php (modify it to your own scenario pls).

Code:
/* Warning: Use AND/OR modify at your own risk. */

function getRootDomain($url) {
    $urlParts = parse_url( $url );
    if ( $urlParts && isset( $urlParts['host'] ) ) {
        return $urlParts['host'];
    }
    return false;
}


function rewriteExternal($html) {
        $domain = str_replace('.', '\.', getRootDomain(get_site_url()));
        //var_dump($domain);
        // The url for your redirection script
        $prefix = 'http://www.example.com?goto=';

        // a regular expression to determine if
        // this link is within your site, edit for your
        // domain or other needs
        $is_internal = '/(?:^\/|^\.\.\/)|'.$domain.'/';

    $dom = new DOMDocument();

    // Parse the HTML into a DOM
    $dom->loadHTML($html);

    $links = $dom->getElementsByTagName('a');

    foreach ($links as $link) {
            $href = $link->getAttribute('href');
            if (!preg_match($is_internal, $href)) {
                $link->getAttributeNode('href')->value = $prefix . $href;
                $link->setAttributeNode(new DOMAttr('rel', 'nofollow'));
                $link->setAttributeNode(new DOMAttr('target', '_blank'));
            }
    }

    // returns the updated HTML or false if there was an error
    return $dom->saveHTML();
}

function outboundLinksModifier(  $content  ) {
    if ( is_main_query() /* && is_singular( 'specific_post_type_or_types' ) */) {
        return  rewriteExternal($content) ;
    }
}
add_action( 'the_content', 'outboundLinksModifier', 10, 2 );

This code will replace all the external links with your chosen redirect URL. Always try keeping things simple. ;)
Thanks mate.
 
Back
Top