0

I have CPT where i do not want to parse shortccode in it's content (using the_content() function). I can use remove_filter to remove default filters for shortcode. but how will i determine that i am removing filter only for my desire CPT?

I have a shortcode [my-custom-shortcode] which i am using in a page. This shortcode use WP_Query and outputting CPT posts. I do not want to parse shortcode inside this CPT posts.

Should i change shortcode with dummy content before shortcode parse hook and replace back after? OR should i remove default filter for shortcode just before my CPT output and again add default filter for shortcodes after my CPT output done?

Or is there any better option?

EDIT: My shortcode looks like this

add_shortcode( 'my-custom-shortcode', 'my_custom_function' );
function my_custom_function(){
    $args = array(
        'post_type' => 'my-cpt',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'order'=> 'ASC'
     );
    $posts = new WP_Query( $args );
    ob_start();

    if( $posts->have_posts() ) {
        while ( $posts->have_posts() ) {
        $posts->the_post();
            the_content(); //Here i do not want to parse (any) shortcode
        }
        wp_reset_postdata();
    }
    return ob_get_clean();
}
8
  • 1
    Instead of disabling the filter, you could update your Shortcode function so that if ( 'your-cpt' === get_post_type() ) { return ''; } - i.e. do/output nothing. I mean, if you could modify the Shortcode function code.
    – Sally CJ
    May 10, 2018 at 4:14
  • 1
    Maybe you misunderstood me. I have updated my question with code. Inside my shortcode content i do not want to parse any shortcode. May 10, 2018 at 5:13
  • Thank you. I have already mentioned that in my question. But sometimes plugins changes this hook priority. That was my concern. Also is parsing shortcode cause any security issue (I am accepting content of this CPT from users)? May 10, 2018 at 5:40
  • So if I now understand it correctly, you don't want your Shortcode to be run in the custom posts loop; right? (Because I know, that would end up with a never-ending loop..) (Sorry, I did not see your comment coming, so I removed my previous comment.)
    – Sally CJ
    May 10, 2018 at 5:52
  • is parsing shortcode cause any security issue - yes, it could. So you should use the shortcode_parse_atts() function, and always sanitize/escape user inputs. E.g. apply esc_url() on URL addresses.
    – Sally CJ
    May 10, 2018 at 5:59

2 Answers 2

0

I believe you need something like this-

add_filter( 'the_content', 'wpse_303180_remove_shortcode' );
function wpse_303180_remove_shortcode( $content ) {
    global $post;
    if( get_post_type( $post ) == 'my-cpt' ) {
        remove_shortcode( 'my-custom-shortcode' );
        // remove_all_shortcodes(); // to remove all shortcodes
    }

    return $content;
}
6
  • Thank you. But as i mentioned in my question i am using my shortcode inside a page. So your if check will fail in this condition. May 10, 2018 at 6:24
  • Just updated it.
    – mukto90
    May 10, 2018 at 6:43
  • Just updated my answer. It'll now prevent from parsing the shortcode in given post_type's content. Please check.
    – mukto90
    May 10, 2018 at 18:40
  • Thank you again. But both remove_shortcode and remove_all_shortcodes remove shortcode from global. So any content comes after my CPT will also lose its shortcode. We could use strip_shortcodes but that will be resource intensive as well as strip shortcode itself. So i used like github.com/shamim2883/front-end-pm/commit/… May 12, 2018 at 7:14
  • Did you test my code?
    – mukto90
    May 12, 2018 at 16:12
0

I am answering my own question for future reference for others who want this behavior

If you want to show shortcode as plain text without parsing follow this procedure

add_shortcode( 'my-custom-shortcode', 'my_custom_function' );
function my_custom_function(){
    global $shortcode_tags;
    $tags = $shortcode_tags;
    $shortcode_tags = array(); //empty global shortcode tags

    $args = array(
        'post_type' => 'my-cpt',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'order'=> 'ASC'
     );
    $posts = new WP_Query( $args );
    ob_start();

    if( $posts->have_posts() ) {
        while ( $posts->have_posts() ) {
        $posts->the_post();
            the_content(); //Here i do not want to parse (any) shortcode
        }
        wp_reset_postdata();
    }
    $shortcode_tags = $tags //reset tags to global variable
    return ob_get_clean();
}

If you want to strip shortcode without parsing follow this procedure

add_filter( 'the_content', 'my_remove_shortcode' );
function my_remove_shortcode( $content ) {
    if( get_post_type() == 'my-cpt' ) {
        $content = strip_shortcodes( $content ); // strip all shortcodes
    }

    return $content;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.