如何使用 URL 中的特定字符串禁用 pages/posts 中的简码?

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

我需要在每个页面或 post 上禁用短代码,其中 url 包含 /?task=delete&postid=

示例url:
博客 url/some 随机符号/?task=delete&postid=一些随机符号

您可以在主题的 functions.php 文件中放置并试用此片段

案例一

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');

更新

案例二

如果你只想删除一些特定的简码而不是全部删除(如果你使用任何简码 based/visual 基于作曲家的主题,这不是一个好主意),你可以使用 remove_shortcode() 函数而不是 remove_all_shortcodes()

示例代码

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

案例三

如果您需要从页面的某些特定部分禁用某些短代码,例如 page/post 内容,您将需要为该特定部分使用过滤器。

示例 1(从内容中删除 所有 短代码)

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' );

示例 2(从内容中删除 一些特定的 简码)

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'