Is there a filter I can use to adjust link URLs before they are rendered?

  1. Home
  2. Knowledge Base
  3. Snippets
  4. Is there a filter I can use to adjust link URLs before they are rendered?

Is there a filter I can use to adjust link URLs before they are rendered?

Starting with Link Whisper 2.3.5, in-content link URLs can be modified to suit your site’s needs by using pre-display filters.

Doing this requires the use of two filters. The first is ‘wpil_add_link_attr_allow_change_url’, and it’s job is to tell Link Whisper that you want to modify link URLs.

The second filter is ‘wpil_add_link_attr_change_url’, and it’s job is to do the actual modifying.

As an example, here is a code snippet that makes sure all internal links have a trailing slash:



// This tells Link Whisper that we want to adjust the URLs in links
add_filter('wpil_add_link_attr_allow_change_url', function(){ return true; });

// This is the code that will add trailling slashes to all internal links
add_filter('wpil_add_link_attr_change_url', 'link_whisper_add_trailing_slashes', 10, 2);
function link_whisper_add_trailing_slashes($url = '', $is_internal = false){
    // if the link is internal
    if($is_internal){
        // make sure it has a trailing slash
        $url = trailingslashit($url);
    }

    // make sure to return the URL
    return $url;
}

To install code such as this, you will need to insert it in your child theme’s functions.php file. If that’s not possible, or you’re not using a child theme, then the next best way to do it is with a PHP Code Snippets plugin.

Build A Powerful Internal Link Strategy Today

Enter your email & we'll send you 8 tips to build an internal link strategy.

And 3 things you should avoid doing with internal links.

    We won't send you spam. Unsubscribe at any time.

    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