wp_targeted_link_rel

The timeline below displays how wordpress function wp_targeted_link_rel has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.6

/**
 * Adds `rel="noopener"` to all HTML A elements that have a target.
 *
 * @since 5.1.0
 * @since 5.6.0 Removed 'noreferrer' relationship.
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') === false || stripos($text, '<a ') === false || is_serialized($text)) {
        return $text;
    }
    $script_and_style_regex = '/<(script|style).*?<\/\1>/si';
    preg_match_all($script_and_style_regex, $text, $matches);
    $extra_parts = $matches[0];
    $html_parts = preg_split($script_and_style_regex, $text);
    foreach ($html_parts as &$part) {
        $part = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $part);
    }
    $text = '';
    for ($i = 0; $i < count($html_parts); $i++) {
        $text .= $html_parts[$i];
        if (isset($extra_parts[$i])) {
            $text .= $extra_parts[$i];
        }
    }
    return $text;
}

WordPress Version: 3.1

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @since 5.1.0
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') === false || stripos($text, '<a ') === false || is_serialized($text)) {
        return $text;
    }
    $script_and_style_regex = '/<(script|style).*?<\/\1>/si';
    preg_match_all($script_and_style_regex, $text, $matches);
    $extra_parts = $matches[0];
    $html_parts = preg_split($script_and_style_regex, $text);
    foreach ($html_parts as &$part) {
        $part = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $part);
    }
    $text = '';
    for ($i = 0; $i < count($html_parts); $i++) {
        $text .= $html_parts[$i];
        if (isset($extra_parts[$i])) {
            $text .= $extra_parts[$i];
        }
    }
    return $text;
}

WordPress Version: 5.3

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @since 5.1.0
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') !== false && stripos($text, '<a ') !== false) {
        if (!is_serialized($text)) {
            $text = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text);
        }
    }
    return $text;
}

WordPress Version: 2.5

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @since 5.1.0
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') === false || stripos($text, '<a ') === false || is_serialized($text)) {
        return $text;
    }
    $script_and_style_regex = '/<(script|style).*?<\/\1>/si';
    preg_match_all($script_and_style_regex, $text, $matches);
    $extra_parts = $matches[0];
    $html_parts = preg_split($script_and_style_regex, $text);
    foreach ($html_parts as &$part) {
        $part = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $part);
    }
    $text = '';
    for ($i = 0; $i < count($html_parts); $i++) {
        $text .= $html_parts[$i];
        if (isset($extra_parts[$i])) {
            $text .= $extra_parts[$i];
        }
    }
    return $text;
}

WordPress Version: 2.3

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @since 5.1.0
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') !== false && stripos($text, '<a ') !== false) {
        $text = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text);
    }
    return $text;
}

WordPress Version: .20

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @since 5.1.0
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') === false || stripos($text, '<a ') === false || is_serialized($text)) {
        return $text;
    }
    $script_and_style_regex = '/<(script|style).*?<\/\1>/si';
    preg_match_all($script_and_style_regex, $text, $matches);
    $extra_parts = $matches[0];
    $html_parts = preg_split($script_and_style_regex, $text);
    foreach ($html_parts as &$part) {
        $part = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $part);
    }
    $text = '';
    for ($i = 0; $i < count($html_parts); $i++) {
        $text .= $html_parts[$i];
        if (isset($extra_parts[$i])) {
            $text .= $extra_parts[$i];
        }
    }
    return $text;
}

WordPress Version: 2.2

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @since 5.1.0
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') !== false && stripos($text, '<a ') !== false) {
        $text = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text);
    }
    return $text;
}

WordPress Version: .10

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @since 5.1.0
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') === false || stripos($text, '<a ') === false || is_serialized($text)) {
        return $text;
    }
    $script_and_style_regex = '/<(script|style).*?<\/\1>/si';
    preg_match_all($script_and_style_regex, $text, $matches);
    $extra_parts = $matches[0];
    $html_parts = preg_split($script_and_style_regex, $text);
    foreach ($html_parts as &$part) {
        $part = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $part);
    }
    $text = '';
    for ($i = 0; $i < count($html_parts); $i++) {
        $text .= $html_parts[$i];
        if (isset($extra_parts[$i])) {
            $text .= $extra_parts[$i];
        }
    }
    return $text;
}

WordPress Version: 5.2

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @since 5.1.0
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') !== false && stripos($text, '<a ') !== false) {
        $text = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text);
    }
    return $text;
}

WordPress Version: 1.4

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') === false || stripos($text, '<a ') === false || is_serialized($text)) {
        return $text;
    }
    $script_and_style_regex = '/<(script|style).*?<\/\1>/si';
    preg_match_all($script_and_style_regex, $text, $matches);
    $extra_parts = $matches[0];
    $html_parts = preg_split($script_and_style_regex, $text);
    foreach ($html_parts as &$part) {
        $part = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $part);
    }
    $text = '';
    for ($i = 0; $i < count($html_parts); $i++) {
        $text .= $html_parts[$i];
        if (isset($extra_parts[$i])) {
            $text .= $extra_parts[$i];
        }
    }
    return $text;
}

WordPress Version: 1.2

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') !== false && stripos($text, '<a ') !== false) {
        $text = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text);
    }
    return $text;
}

WordPress Version: .10

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') === false || stripos($text, '<a ') === false || is_serialized($text)) {
        return $text;
    }
    $script_and_style_regex = '/<(script|style).*?<\/\1>/si';
    preg_match_all($script_and_style_regex, $text, $matches);
    $extra_parts = $matches[0];
    $html_parts = preg_split($script_and_style_regex, $text);
    foreach ($html_parts as &$part) {
        $part = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $part);
    }
    $text = '';
    for ($i = 0; $i < count($html_parts); $i++) {
        $text .= $html_parts[$i];
        if (isset($extra_parts[$i])) {
            $text .= $extra_parts[$i];
        }
    }
    return $text;
}

WordPress Version: 5.1

/**
 * Adds rel noreferrer and noopener to all HTML A elements that have a target.
 *
 * @param string $text Content that may contain HTML A elements.
 * @return string Converted content.
 */
function wp_targeted_link_rel($text)
{
    // Don't run (more expensive) regex if no links with targets.
    if (stripos($text, 'target') !== false && stripos($text, '<a ') !== false) {
        $text = preg_replace_callback('|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text);
    }
    return $text;
}