Using WordPress ‘category_link’ PHP filter

The category_link WordPress PHP filter allows you to modify the category link URL.

Usage

add_filter('category_link', 'your_custom_function', 10, 2);

function your_custom_function($termlink, $term_id) {
  // Your custom code here
  return $termlink;
}

Parameters

  • $termlink (string): The category link URL.
  • $term_id (int): The term ID.

More information

See WordPress Developer Resources: category_link

Examples

This example adds a custom prefix “my-category/” to category links.

add_filter('category_link', 'prefix_category_link', 10, 2);

function prefix_category_link($termlink, $term_id) {
  return str_replace('/category/', '/my-category/', $termlink);
}

This example appends a query parameter “?source=blog” to the category links.

add_filter('category_link', 'append_query_parameter', 10, 2);

function append_query_parameter($termlink, $term_id) {
  return $termlink . '?source=blog';
}

This example replaces the domain of the category links with “https://example.com“.

add_filter('category_link', 'custom_domain_category_link', 10, 2);

function custom_domain_category_link($termlink, $term_id) {
  return preg_replace('#^https?://[^/]+#', 'https://example.com', $termlink);
}

This example converts the category link URL to uppercase.

add_filter('category_link', 'uppercase_category_link', 10, 2);

function uppercase_category_link($termlink, $term_id) {
  return strtoupper($termlink);
}

This example adds a custom suffix “.html” to category links.

add_filter('category_link', 'suffix_category_link', 10, 2);

function suffix_category_link($termlink, $term_id) {
  return $termlink . '.html';
}