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?
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.