Using preg_replace to wrap HTML tags in Shortcode IF post is in Category


I am trying to wrap HTML tags in a shortcode IF the post is assigned to a specific category.
Problem is; when using it like this – no content at all is shown no matter the post and / or what category it is assigned to.

I just want to wrap the HTML tags if the post is assigned to the WordPress category.

add_filter( 'the_content', 'wrap_tags_in_shortcode_if_in_cat' , 9 );
function wrap_tags_in_shortcode_if_in_cat( $content ) {

    if ( in_category( 'WordPress' ) ) {

        $content = preg_replace('/(<pre[^>]*>s*<code[^>]*>)/',"[protected]$1", $content);

        $content =  preg_replace('/(</code>s*</pre>)/', "$1[/protected]", $content);

    return $content;
Ben Matthew 4 months 0 Answers 15 views 0

Leave an answer