Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use pre_category_nicename filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
February 2, 2023
5 minutes read

pre_category_nicename filter

Using the ‘pre_$taxonomy_$field’ hook can be really handy. To use the `pre_category_nicename` filter, you’ll first need to register it using `add_filter`. You can drop this code into the `functions.php` file of your active theme or in a custom WordPress Plugin.

At WePlugins, we always recommend creating a custom WordPress Plugin when using hooks. This way, nothing breaks when you update your WordPress Theme in the future.

In the example below, we’ve defined a function `weplugins_modify_pre_category_nicename_defaults` which takes one parameter. We registered this using `add_filter`. The first parameter `pre_category_nicename` is the name of the hook, the second parameter `weplugins_modify_pre_category_nicename_defaults` is the name of the function to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed to the registered function.

Sometimes, you might need to remove a registered hook, so you can use `remove_filter` to remove the `pre_category_nicename` filter.

Parameters

    Below is the one parameter required to use this hook.

  • $value: (string) The category nicename.

Live Example 1

Below is an example of how you can use this hook.

    function weplugins_modify_pre_category_nicename_defaults($value) {
        // Update the $value variable according to your website requirements and return this variable.
        // You can modify the $value variable conditionally too if you want.
        return $value;
    }
    // Add the filter
    add_filter("pre_category_nicename", "weplugins_modify_pre_category_nicename_defaults", 10, 1);
    

Live Example 2

To remove a hook callback, use the example below.

    remove_filter("pre_category_nicename", "weplugins_modify_pre_category_nicename_defaults", 10, 1);
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Live Example 3

Another practical example where you might want to change the category nicename based on some custom logic.

    function weplugins_custom_pre_category_nicename($value) {
        if($value == 'old-nicename') {
            $value = 'new-nicename';
        }
        return $value;
    }
    // Add the filter
    add_filter("pre_category_nicename", "weplugins_custom_pre_category_nicename", 10, 1);
    

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook or need customization, please contact us and we’d be happy to assist you.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

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.