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'>
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
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');