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
Add a custom prefix to category links
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); }
Append query parameter to category links
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'; }
Add a custom domain to category links
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); }
Convert category links to uppercase
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); }
Add a custom suffix to category links
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'; }