Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1365249

    Is it possible to add a custom class to the social links? I need to add this class “external-link” to all external links to maintain legal compliance.
    I appreciate the help!

    <a class="external-link" target='_blank' href='https://twitter.com/amerisave' aria-hidden='true' data-av_icon='' data-av_iconfont='entypo-fontello' title='Twitter'>

    #1365289

    you can do it via child-theme functions.php :

    function add_class_to_external_links(){
    ?>
    <script>
    (function($){
      $('a[target*="_blank"]').each(function(){
            $(this).addClass('external-link');
        });
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'add_class_to_external_links');

    if you like to have it on all elements just remove the anchor a from the snippet above

    #1365291

    or if you forgot to set the _blank target option – maybe better to list all links except some links that fit to your domain url or mailto links etc.

    function add_class_to_external_links(){
    ?>
    <script>
    (function($){
    	var url = window.location.origin;
    	$('a').not('a[href*="'+url+'"], a[href*="mailto:"], a[href*="#"], a[href*="tel:"], a[href*="javascript:;"] ').each(function(){
    		$(this).addClass('external-link');
    	});
    })(jQuery);
    </script>
    <?php
    }
    add_action('wp_footer', 'add_class_to_external_links');
    #1365320

    Hi,

    Thanks for helping out @guenni007 :-)

    Best regards,
    Rikard

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