Title: php in category description
Last modified: August 21, 2016

---

# php in category description

 *  Resolved [s.clause](https://wordpress.org/support/users/sclause/)
 * (@sclause)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/php-in-category-description/)
 * Does someone have solution for adding PHP code into category description? I am
   trying to find something on internet over 4 hours, but without success. I really
   need to put PHP code into category description. I have multiple categories and
   for each one of them i need a different PHP code into description. Is there some
   way how to do it?
 * I really need help with this, i don’t know what else to do.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/php-in-category-description/#post-4787462)
 * 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.
 *  Thread Starter [s.clause](https://wordpress.org/support/users/sclause/)
 * (@sclause)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/php-in-category-description/#post-4787500)
 * 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?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/php-in-category-description/#post-4787523)
 * 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;
       }
       ```
   
 *  Thread Starter [s.clause](https://wordpress.org/support/users/sclause/)
 * (@sclause)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/php-in-category-description/#post-4787534)
 * 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.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/php-in-category-description/#post-4787554)
 * 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.
 *  Thread Starter [s.clause](https://wordpress.org/support/users/sclause/)
 * (@sclause)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/php-in-category-description/#post-4787568)
 * 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.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘php in category description’ is closed to new replies.

## Tags

 * [category description](https://wordpress.org/support/topic-tag/category-description/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 6 replies
 * 2 participants
 * Last reply from: [s.clause](https://wordpress.org/support/users/sclause/)
 * Last activity: [12 years, 1 month ago](https://wordpress.org/support/topic/php-in-category-description/#post-4787568)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
