如何在URL中使用特定字符串禁用页面/帖子中的短代码?

问题描述:

我需要每一页或交其网址中包含/?task=delete&postid=如何在URL中使用特定字符串禁用页面/帖子中的短代码?

示例URL上禁用的简:
博客网址/一些随机的符号/ ?task=delete&postid=一些随机的符号

可以放置并尝试这个片段在你的主题functions.php文件

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

更新

案例2,如果你只想删除,而不是删除所有(这是不是一个好主意,如果你正在使用任何短代码基础/视觉作曲家基于主题),您可以将某些特定的简

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

替换NOT_NEEDED_SHORTCODE_STRING你要删除的简码字符串

案例3

如果您需要从页面的某些特定部分禁用一些简码,如例如从页/后的内容,你会需要为该特定部分使用过滤器。

实施例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'); 

替换为NOT_NEEDED_SHORTCODE_STRING嘘ortcode字符串要删除

这个例子是关于从页/后的“内容”部分删除简码。如果您想将其应用于某个其他部分,则需要使用相关的过滤器挂钩标记替换add_filter('the_content', 'remove_shortcode_exec_on_query');处的挂钩标记'the_content'。例如对于标题,这将是'the_title'

+0

它几乎没有诀窍:)但您的代码也禁用短代码显示页面本身(它是一个插件),所以我只看到现在简码为页面内容。我认为我们需要添加条件来仅从帖子/页面内容部分删除简码,而不是整个页面。 – Ted

+0

我明白了,我误解了,所以,如果你只需要禁用简码的内容的一部分,您可以使用此过滤器钩子, '函数remove_shortcode_exec_on_query($内容){ 如果(空($ _GET [“任务” ])|| empty($ _GET [“postid”])||“delete”!== $ _GET [“task”]) return $ content; remove_all_shortcodes(); return $ content; } 的add_filter(“the_content”,“remove_shortcode_exec_on_query”,10,1);' –

+0

那一个打破了我的网站 – Ted