Execute PHP in Category/Tag description

We all know that there are plenty of plugins and hacks that allows execution of PHP in posts/pages and sidebars.
But how to execute PHP in category/tag description in WordPress?

I simply want to allow PHP codes in category/tag description so that I can show them in Category/Tags pages like <?php echo category_description(); ?>.

Read More

Related posts

Leave a Reply

2 comments

  1. By Default when category_description() gets called, it is filtered by code.
    But I suggest you to remove filter by adding them in functions.php of theme. Try something like this. I use it for html tags

    remove_filter( 'pre_term_description', 'wp_filter_kses' );
    remove_filter( 'term_description', 'wp_kses_data' ); 
    
  2. The easiest way, without headaches, is to use a shortcode and send all variables as attributes, like:

    Description of the category: [php var1=”33″ var2=”string” var3=”array&array&array”]shortcode content[/php].

    And then parse all the $atts and content doing any PHP you want. To enable shortcodes in the category description, use:

    add_filter( 'category_archive_meta', function( $content ){
        return apply_filters( 'the_content', $content );
    });
    

    A test shortcode:

    add_shortcode( 'php', function( $atts, $content ){
        return sprintf( 
            "<pre><code>%snn%s</code></pre>", 
            $content, 
            print_r( $atts, true ) 
        );
    });
    

    And the result using the sample description above:

    shortcode php