• Resolved hamadali5

    (@hamadali5)


    hi everyone

    i need to add the rel=”sponsored” attribute to old affiliate links in my websites as google said to avoid algorithmic penalty ,

    Most of those links were created as buttons using the Shortcodes Ultimate plugin.

    i tried this code in the snippet plugin:

    function add_sponsored_rel_attribute($content) {
    // Define an array of allowed domain keywords
    $allowed_domain_keywords = array(
    ‘amazon’,
    ‘jumia’
    // Add more keywords as needed
    );

    // Add rel="sponsored" to external links for domains containing allowed keywords
    $content = preg_replace_callback(
        '/<a[^>]+href=[\'"]([^\'"]+)[\'"][^>]*>/i',
        function ($matches) use ($allowed_domain_keywords) {
            $link_url = $matches[1];
    
            // Check if the link is external
            $link_domain = parse_url($link_url, PHP_URL_HOST);
    
            // Check if the link's domain contains any allowed keyword
            foreach ($allowed_domain_keywords as $keyword) {
                if (stripos($link_domain, $keyword) !== false) {
                    // Add rel="sponsored"
                    return '<a href="' . $link_url . '" rel="sponsored">';
                }
            }
    
            return $matches[0];
        },
        $content
    );
    
    return $content;

    }

    // Hook the function to the_content filter
    add_filter(‘the_content’, ‘add_sponsored_rel_attribute’);
    add_filter(‘widget_text_content’, ‘add_sponsored_rel_attribute’);

    ——————————-

    the above code works fine for all external links containing the keywords i provided in the code.

    but it doesn’t work for the external links were created buttons in from the Shortcodes Ultimate plugin ??

    i tried differents codes, but all were unable to add the rel=”sponsored” attribute to the Shortcodes Ultimate Buttons?

    please help me.


  • The topic ‘add rel=”sponsored ” to all old affiliate links in my site’ is closed to new replies.