How to add CSS classes to Link Whisper links

  1. Home
  2. Knowledge Base
  3. Technical Guides
  4. How to add CSS classes to Link Whisper links

How to add CSS classes to Link Whisper links

You can add CSS classes to the links Link Whisper creates by using a filter called “wpil_link_classes”. The filter accepts either a string of class names or an array of class names.

The class names will be added to the links when they’re inserted in the content and will be permanent. Removing the code that adds the class names or Link Whisper won’t remove the class names from the links.

To add a single class to all the links that Link Whisper creates, add a snippet like this to your site’s functions.php file.

add_filter('wpil_link_classes', 'wpil_add_link_classes', 10, 2); 
function wpil_add_link_classes($classes, $external_link){ 
    $classes = 'link-whisper-test-class'; 
    return $classes; 
}

This will add a class called “link-whisper-test-class” to all the links that Link Whisper creates. It can be any class name you would like.

You can specify multiple class names by adding them to the $classes string, like this:

add_filter('wpil_link_classes', 'wpil_add_link_classes', 10, 2); 
function wpil_add_link_classes($classes, $external_link){ 
    $classes = 'link-whisper-test-class a-second-test-class'; 
    return $classes; 
}

You can also specify multiple classes by passing an array of class names, like this:

add_filter('wpil_link_classes', 'wpil_add_link_classes', 10, 2); 
function wpil_add_link_classes($classes, $external_link){ 
    $classes = array('link-whisper-test-class', 'a-second-test-class'); 
    return $classes; 
}

Either method for adding class names works equally well, and there is no preferred way to add class names.

Finally, if you want to apply class names to just external links or just to internal links, you can selectively apply class names by checking the $external_link variable. For internal links, the variable will be FALSE. For external links, it will be TRUE.

For example, you can add a class only to external links like this:

add_filter('wpil_link_classes', 'wpil_add_link_classes', 10, 2); 
function wpil_add_link_classes($classes, $external_link){
    // if this is an external link
    if($external_link){
        // add the external link class
        $classes = 'link-whisper-external-link'; 
    }

    return $classes; 
}

 

Was this article helpful?

Need Support?

Can't find the answer you're looking for?
Contact Support

Get Started with LinkWhisper

Speed Up the Process of Internal Linking and Help You Rank Better in Google

Get LinkWhisper Now