Search code examples
wordpressshortcode

How to disable shortcodes in pages/posts with a certain string in the URL?


I need to disable shortcodes on every page or post which url contains /?task=delete&postid=

Example url:
blog url/some random symbols/?task=delete&postid=some random symbols


Solution

  • you can place and try this snippet in your theme's functions.php file

    Case 1

    function remove_shortcode_exec_on_query(){
    
        // condition(s) if you need to decide not to disabling shortcode(s)
        if( empty( $_GET["task"] ) || empty( $_GET["postid"] ) || "delete" !== $_GET["task"] )
    
            return;
    
        // Condition(s) at top are not met, we can remove the shortcode(s)
        remove_all_shortcodes();    
    }
    
    add_action('wp','remove_shortcode_exec_on_query');
    

    Update

    Case 2

    if you want to remove only some specific shortcodes instead of removing all (which is not a good idea if you are using any shortcode based/visual composer based theme), you can use remove_shortcode() function instead of remove_all_shortcodes()

    example code

    function remove_shortcode_exec_on_query(){
    
        // condition(s) if you need to decide not to disabling shortcode(s)
        if( empty( $_GET["task"] ) || empty( $_GET["postid"] ) || "delete" !== $_GET["task"] )
    
            return;
    
        // Condition(s) at top are not met, we can remove the shortcode(s)
        remove_shortcode('NOT_NEEDED_SHORTCODE_STRING_1');
        remove_shortcode('NOT_NEEDED_SHORTCODE_STRING_2');    
    }
    
    add_action('wp','remove_shortcode_exec_on_query');
    

    Replace NOT_NEEDED_SHORTCODE_STRING with the shortcode string you want to remove

    Case 3

    If you need to disable some shortcodes from some specific part of the page, as example from page/post content, you will need to use filters for that specific part.

    Example 1 ( removing all shortcodes from content )

    function remove_shortcode_exec_on_query( $content ) {
    
        // condition(s) if you need to decide not to disabling shortcode(s)
        if( empty( $_GET["task"] ) || empty( $_GET["postid"] ) || "delete" !== $_GET["task"] ) 
    
            return $content;
    
       // Condition(s) at top are not met, we can remove the shortcode(s)
       return strip_shortcodes( $content );
    }
    add_filter( 'the_content', 'remove_shortcode_exec_on_query' );
    

    Example 2 (Removing some specific shortcodes from content)

    function remove_shortcode_exec_on_query( $content ) {
    
        // condition(s) if you need to decide not to disabling shortcode(s)
        if( empty( $_GET["task"] ) || empty( $_GET["postid"] ) || "delete" !== $_GET["task"] ) 
    
            return $content;
    
       // Condition(s) at top are not met, we can remove the shortcode(s)
       remove_shortcode('NOT_NEEDED_SHORTCODE_STRING_1');
       remove_shortcode('NOT_NEEDED_SHORTCODE_STRING_2');
    
       return $content;
    }
    add_filter( 'the_content', 'remove_shortcode_exec_on_query' );
    

    Replace NOT_NEEDED_SHORTCODE_STRING with the shortcode string you want to remove

    This example is about removing shortcode from "content" part of the page/post. if you want to apply it to some other part, the hook tag 'the_content' at add_filter( 'the_content', 'remove_shortcode_exec_on_query' ); will need to replaced by relevant filter hook tag. e.g. for title, it will be 'the_title'