• Resolved Opace Digital Agency

    (@opacewebdesign)


    Hi, thanks for creating the plugin. Overall, it’s excellent!

    I’m using the plugin to add internal links to old neglected blog posts, but I’m having a bit of an issue with the “Number of links” setting. I’ve got this set to “1” and can see it doesn’t attempt to automate the same link more than once, which is great. However, it’s not detecting if a link has already been added manually, which results in duplicates.

    Take this post for example: https://opace.agency/blog/magento-designers-ecommerce-success

    It already links to https://opace.agency/services/ecommerce/magento-web-design-development using the anchor “Magento web designers” – this must have been added manually back in 2013 but the plugin is now adding another link to the same URL using “Magento designers”.

    Obviously, we want to avoid this as it looks like link spamming. Sadly, our older posts suffer from both under linking and excessive linking, but this just makes the excessive linking issue worse.

    Hopefully I’m just missing something?

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author webraketen

    (@webraketen)

    Hey, thanks for reaching out. We are glad you like our plugin!

    Unfortunately our plugin cannot currently recognise whether links have already been set manually in advance. However, we have taken up the idea internally and will evaluate whether we will integrate such a feature in the future.

    Thread Starter Opace Digital Agency

    (@opacewebdesign)

    Thanks. I’ve fund a functions.php workaround for this. I don’t know if it’s the best solution but sharing below:


    // Helper function to normalize URLs for comparison
    function normalize_url($url) {
    $parsed_url = parse_url(strtolower($url));
    $host = isset($parsed_url['host']) ? preg_replace('/^www./', '', $parsed_url['host']) : '';
    $path = isset($parsed_url['path']) ? rtrim($parsed_url['path'], '/') : '';
    return $host . $path;
    }

    // Function to remove duplicate internal links
    function remove_duplicate_internal_links($content) {
    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->loadHTML('' . $content);
    $xpath = new DOMXPath($dom);
    $links = [];
    $nodes_to_remove = [];

    // Collect all links and identify duplicates
    foreach ($xpath->query('//a[@href]') as $node) {
    $href = $node->getAttribute('href');
    $normalized_href = normalize_url($href);

    if (isset($links[$normalized_href])) {
    // Duplicate link found, mark it for removal
    $nodes_to_remove[] = $node;
    } else {
    $links[$normalized_href] = true;
    }
    }

    // Remove duplicate link nodes
    foreach ($nodes_to_remove as $node) {
    // Replace the link with its text content
    $text_node = $dom->createTextNode($node->textContent);
    $node->parentNode->replaceChild($text_node, $node);
    }

    // Get the updated content
    $body = $dom->getElementsByTagName('body')->item(0);
    $content = '';
    if ($body) {
    foreach ($body->childNodes as $childNode) {
    $content .= $dom->saveHTML($childNode);
    }
    } else {
    $content = $dom->saveHTML();
    }

    return $content;

    }
    // Add this filter with a high priority to run after other content filters
    add_filter('the_content', 'remove_duplicate_internal_links', 9999);

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.