Using WordPress ‘index_rel_link’ PHP filter

The index_rel_link WordPress PHP filter allows you to modify the default “index” link in the <head> section of your website.

Usage

add_filter('index_rel_link', 'my_custom_index_rel_link');

function my_custom_index_rel_link($link) {
    // your custom code here
    return $link;
}

Parameters

  • $link (string) – The default “index” link value.

More information

See WordPress Developer Resources: index_rel_link

Examples

Modify the default “index” link to point to a custom URL.

add_filter('index_rel_link', 'change_index_link_to_custom_url');

function change_index_link_to_custom_url($link) {
    $link = '<link rel="index" href="https://www.example.com/custom-url/" />';
    return $link;
}

Remove the “index” link from the <head> section of your website.

add_filter('index_rel_link', '__return_false');

Add a “nofollow” attribute to the “index” link.

add_filter('index_rel_link', 'add_nofollow_to_index_link');

function add_nofollow_to_index_link($link) {
    $link = str_replace('rel="index"', 'rel="index nofollow"', $link);
    return $link;
}

If your website is multilingual, you can change the “index” link URL based on the current language.

add_filter('index_rel_link', 'change_index_link_based_on_language');

function change_index_link_based_on_language($link) {
    $language = get_locale();

    if ($language == 'en_US') {
        $link = '<link rel="index" href="https://www.example.com/en/" />';
    } elseif ($language == 'fr_FR') {
        $link = '<link rel="index" href="https://www.example.com/fr/" />';
    }

    return $link;
}

Change the “index” link to point to the blog homepage when viewing single posts.

add_filter('index_rel_link', 'change_index_link_to_blog_home_on_single_posts');

function change_index_link_to_blog_home_on_single_posts($link) {
    if (is_single()) {
        $link = '<link rel="index" href="' . get_permalink(get_option('page_for_posts')) . '" />';
    }
    return $link;
}