A
A
Albinor2018-05-09 20:03:30
WordPress
Albinor, 2018-05-09 20:03:30

How to redirect a link?

How to redirect outgoing links like this " rel="nofollow" href=/out/?url= " ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor Vorotnev, 2018-05-10
@HeadOnFire

First, you need to create a page with the 'out' slug or a custom rewrite rule with the same slug.
Second, we need to teach WP to recognize the 'url' GET variable.
Thirdly, on this page or request (in the case of a rewrite rule), you need to add a function that will check this 'url' GET variable, make sure that it is safe and correct, and perform a redirect.
This is a purely functional part, the handler of the redirect itself by such a scheme.
The second part will be generating the correct links, given the attribute. And here it is already necessary to clarify many points - where the links will be placed, as well as so on.
I have described the logic of work to you here, a complete answer with examples of code pieces is beyond the scope of the Toaster, this is a task for freelancing. If you want to try to solve it yourself - re-read the logic and start working. As more narrow and specific questions appear, ask.

O
Orkhan Hasanli, 2018-05-10
@azerphoenix

As I understand it, you need to add the nofollow attribute to external links. I myself use the following code:
Add to functions.php
Before adding, replace md7.info with the address of YOUR site

// nofollow noopener noreferrer
function add_nofollow_content($content) 
{	
  $content = preg_replace_callback(
    '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
  function($m) {
    if (strpos($m[1], "md7.info") === false)
    return '<a href="'.$m[1].'" rel="nofollow noopener noreferrer" target="_blank">'.$m[2].'</a>';
    else
    return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
  },
  $content);
    return $content;
  }
add_filter('the_content', 'add_nofollow_content');

PS If you are using ACF and want to also filter content and add nofollow then add this code too:
// nofollow noopener noreferrer for ACF
function add_nofollow_acf($field) {
  $field = preg_replace_callback(
    '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
  function($m) {
    if (strpos($m[1], "md7.info") === false)
    return '<a href="'.$m[1].'" rel="nofollow noopener noreferrer" target="_blank">'.$m[2].'</a>';
    else
    return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
  },
  $field);
    return $field;
  }
add_filter('acf/load_value', 'add_nofollow_acf');

A
Albinor, 2018-05-10
@Albinor

how to make a link like this href=/out/?url= " ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question