Using WordPress ‘paginate_links’ PHP filter

The paginate_links WordPress PHP filter allows you to modify the paginated links for your archive pages.

Usage

add_filter('paginate_links', 'your_custom_function');
function your_custom_function($link) {
  // your custom code here
  return $link;
}

Parameters

  • $link (string) – The paginated link URL.

More information

See WordPress Developer Resources: paginate_links

Examples

Add a custom CSS class to all paginated links for easy styling.

add_filter('paginate_links', 'add_css_class_to_paginate_links');
function add_css_class_to_paginate_links($link) {
  $link = str_replace('<a', '<a class="my-custom-class"', $link);
  return $link;
}

Prevent search engines from following paginated links by adding a nofollow attribute.

add_filter('paginate_links', 'add_nofollow_to_paginate_links');
function add_nofollow_to_paginate_links($link) {
  $link = str_replace('<a', '<a rel="nofollow"', $link);
  return $link;
}

Add a unique id attribute to each paginated link.

add_filter('paginate_links', 'add_id_to_paginate_links');
function add_id_to_paginate_links($link) {
  static $counter = 1;
  $link = str_replace('<a', '<a id="paginate-link-' . $counter . '"', $link);
  $counter++;
  return $link;
}

Disable the link for the current page in the pagination by removing the href attribute.

add_filter('paginate_links', 'remove_href_from_current_page');
function remove_href_from_current_page($link) {
  global $paged;

  if (strpos($link, 'current') !== false) {
    $link = str_replace(' href=', ' data-', $link);
  }

  return $link;
}

Add a custom data attribute to all paginated links for usage with JavaScript or other purposes.

add_filter('paginate_links', 'add_data_attribute_to_paginate_links');
function add_data_attribute_to_paginate_links($link) {
  $link = str_replace('<a', '<a data-custom-attr="my-value"', $link);
  return $link;
}