plugin development - add_action pre_term_description vs. pre_category_description

admin2025-06-05  3

I am trying to set a default tag and a (different) default category description in case there is none given on creation.

I have found the action "pre_category_description", but no equivalent for the tags. The only thing that I could find is "pre_term_description", but that one behaves weird.

I have added the following code to my plugin:

add_action('pre_term_description', 'default_term');
function default_term($description) {
    if ($description == ''){
        $description_new = "default term description";
    } else {
        $description_new = $description;
    }
    error_log("setting term description");
    return $description_new;
}

add_action('pre_category_description', 'default_category');
function default_category($description) {
    if ($description == ''){ // we only replace in case there is a setting and no input
        $description_new = "default category description";
    } else {
        $description_new = $description;
    }
    error_log("setting category description");
    return $description_new;
}

However, it seems that pre_term description is being executed for both tags AND categories, and the term description overrules the category one, although it's executed last.

If I add a new tag, I get this log entry:

[06-Dec-2018 07:44:54 UTC] setting term description

and the resulting description is "default term description"

if I add a new category, I get this here:

[06-Dec-2018 07:44:54 UTC] setting term description
[06-Dec-2018 07:44:54 UTC] setting category description

and the resulting description is ALSO "default term description"

How can I make a differentiation if the edited item is a category or a tag so that this works properly?

I am trying to set a default tag and a (different) default category description in case there is none given on creation.

I have found the action "pre_category_description", but no equivalent for the tags. The only thing that I could find is "pre_term_description", but that one behaves weird.

I have added the following code to my plugin:

add_action('pre_term_description', 'default_term');
function default_term($description) {
    if ($description == ''){
        $description_new = "default term description";
    } else {
        $description_new = $description;
    }
    error_log("setting term description");
    return $description_new;
}

add_action('pre_category_description', 'default_category');
function default_category($description) {
    if ($description == ''){ // we only replace in case there is a setting and no input
        $description_new = "default category description";
    } else {
        $description_new = $description;
    }
    error_log("setting category description");
    return $description_new;
}

However, it seems that pre_term description is being executed for both tags AND categories, and the term description overrules the category one, although it's executed last.

If I add a new tag, I get this log entry:

[06-Dec-2018 07:44:54 UTC] setting term description

and the resulting description is "default term description"

if I add a new category, I get this here:

[06-Dec-2018 07:44:54 UTC] setting term description
[06-Dec-2018 07:44:54 UTC] setting category description

and the resulting description is ALSO "default term description"

How can I make a differentiation if the edited item is a category or a tag so that this works properly?

Share Improve this question asked Dec 6, 2018 at 7:50 uncoveryuncovery 19911 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Categories and tags are both terms, which is why your code is running twice.

What you need to is use the second argument passed to the callbacks for the pre_term_description hook, which tells you the taxonomy the filter is currently being applied for:

function wpse_321166_default_term_description( $description, $taxonomy ) {
    if ( $description ) {
        return $description;
    }

    switch ( $taxonomy ) {
        case 'category':
            $description = 'Setting category description';
            break;
        case 'post_tag':
            $description = 'Setting tag description';
            break;
    }

    return $description;
}
add_action( 'pre_term_description', 'wpse_321166_default_term_description', 10, 2);

The documentation for the hook was a bit hard to fund, because it's got a variable name, but you can see how the taxonomy parameter is available here.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749124649a316573.html

最新回复(0)