You can’t put PHP in a description field. If you could, it would be a huge security flaw! Fortunately, you can use the ‘category_description’ filter (applied in sanitize_term_field() to apply to the description whatever logic you would have entered as PHP in the field.
To apply different logic for each term you can use a switch/case structure where each case is a different term.
Thank you for your reply, but i am afraid, that i don’t really know, how to use sanitize_term_field(). Can you tell me, how should i do it, or how should i use it? Or if you can, direct me somewhere, where can i read about it?
Without knowing what PHP you need to apply, I can’t be too specific, but I’ll show you the basic framework. The main problem with this approach is you would need to edit this code every time someone adds a term that needs PHP logic applied. Executing input as PHP involves using eval() which is where the security issues lie. This code will run anytime category_description() is called (when $context=='display') and likely under several other contexts as well.
// filter does not fire when $context == ('raw'|'db'|'rss'|'edit')
add_filter('category_description', 'scl_fix_cat_description', 10, 3 );
function scl_fix_cat_description( $description, $term_id, $context ) {
switch ( $term_id ) {
case 11:
//do something when the category term ID==11
break;
case 22:
//do something when the category term ID==22
break;
default:
//do something for any other category terms
}
return $description;
}
Well, i need to add PHP code for counting posts in one specific category. I have a website, where i have multi language plugin (WPML) and i need to show posts count in categories and then also in subcategories. So user can choose one category and there will be post count for this category right below description and when user choose a different category or subcategory, it will be the same (post count below category description).
For this i use PHP code _get_category_count(‘category-name’). It should work without category name so i could add it into category template, but without category name it counts wrong, so thats why i’m searching for solution for adding PHP code into category description. It would let me add post count for specific category without searching way, how to show PHP code only in one category.
EDIT: I tried to apply your code, and it worked, but it shows post count 3 times and before description. 2 times before whole website (like error) and one time before description where it should be, but i need to add it after description.
Yes, the problem with my code is you’d need to figure out ways to filter out all the other reasons the filter fires. (I didn’t think it’d be that many though!) One way is to add the filter just before it is needed, then remove it again immediately after.
Is it really necessary to actually add the count to the description itself? Why couldn’t you edit the category archive template to display the count immediately after (or where ever) the description? Something like this perhaps:
echo category_description();
$count = _get_category_count( get_queried_object_id() )
echo "<br>\n$count posts are in this category<br>\n";
Alternately, some themes have a filter for the description output you may be able to use.
Well, it is complex website and it is hard to explain. I really needed to figure this out, because it is also multilanguage website, and it was really dificult to put post count on the right place and right category in each language with right posts count (WP always counted it wrong). But i also had an idea, that i could make widget for it and thanks to author of plugin “display widgets” i was able to set it just like i wanted to. So thank you so much for your help and your time which you spent on my problem.