This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
So, you’re diving into the world of WordPress hooks, and today we’re talking about the edit_term_link filter. This hook lets you filter the anchor tag for the edit link of a term. It’s pretty handy when you want to customize how those edit term links appear on your site.
To use the edit_term_link filter, you first need to register it using add_filter. You can throw this code into the functions.php file of your active theme or, better yet, create a custom WordPress plugin. At WePlugins, we always recommend creating a custom plugin so that your changes stick around even after theme updates.
Here’s a quick rundown: we’ll define a function named modify_edit_term_link_defaults that takes two parameters. Then, we’ll register it using add_filter. The first parameter is the name of the hook (edit_term_link), the second is the function to call (modify_edit_term_link_defaults), the third is the priority (useful if the same hook is called multiple times), and the fourth is the number of arguments to pass.
Sometimes, you might want to remove a registered hook, and for that, you can use remove_filter to remove the edit_term_link filter.
Parameters
- $link: (string) The anchor tag for the edit link.
- $term_id: (int) Term ID.
Below are the two parameters required to use this hook.
Live Example
apply_filters( 'edit_term_link', string $link, int $term_id )
Check out these examples to see how you can use this hook:
Basic Example
In this example, we simply modify the edit term link using the modify_edit_term_link_defaults function.
function weplugins_modify_edit_term_link_defaults($link, $term_id) { // Update the $link variable according to your website requirements and return this variable. // You can modify the $link variable conditionally too if you want. return $link; } // add the filter add_filter( "edit_term_link", "weplugins_modify_edit_term_link_defaults", 10, 2 );
Conditional Modification
Here, we modify the edit term link based on a condition. For instance, if the term ID is 5, we change the link.
function weplugins_modify_edit_term_link_conditionally($link, $term_id) { if ($term_id == 5) { $link = str_replace('edit', 'custom-edit', $link); } return $link; } // add the filter add_filter( "edit_term_link", "weplugins_modify_edit_term_link_conditionally", 10, 2 );
Removing the Hook
If you need to remove the hook callback, use the example below. Make sure you provide the same callback function name, priority, and number of arguments.
remove_filter( "edit_term_link", "weplugins_modify_edit_term_link_defaults", 10, 2 );
Contact Us
If you need any customization or run into any issues, feel free to reach out to us. Visit our Contact Page and we’d be happy to assist you.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.