首页 文章

在Wordpress中禁用所有样式,脚本和元标记的最佳方法

提问于
浏览
2

我有一个博客(使用 twentyfifteen 子主题)和一个页面,我必须禁用由WordPress添加的所有样式,脚本和元标记,除了由 All in One Seo Pack plugin. 添加的元标记

我是Wordpress的新手,我试图在这个页面的模板中使用 define('WP_USE_THEMES', false) ,没有任何事情发生(视觉上) . 我知道有像 wp_dequeue_style() 这样的函数,但我无法检入 functions.php 当前页面是否是上述页面 .

实现这一目标的最佳方法是什么?

1 回答

  • 3

    有两个功能

    • style_dequeue_function() 它将删除所有样式 .

    • script_dequeue_function() 它将删除所有脚本 .

    所有你需要提供页面slug或模板名称 .

    $ pageSlug您要隐藏WordPress样式和脚本 .

    OR

    $ Template_Name按模板名称

    并且 remove_action 将删除WordPress生成器标记 .

    remove_action('wp_head', 'wp_generator');
    

    注意:它只会使用WP enque函数添加的deque样式或脚本 .

    例如:

    wp_register_script( 'site', get_template_directory_uri().'/js/site.js', array( 'jquery' ) );
    wp_enqueue_script( 'site' );
    wp_register_style( 'screen', get_template_directory_uri().'/style.css', '', '', 'screen' );
    wp_enqueue_style( 'screen' );
    
    
    function style_dequeue_function()
    {
        global $wp_styles;
    
        $array = array();
        // Runs through the queue styles
        foreach ($wp_styles->queue as $handle) :
            $array[] = $handle;
        endforeach;
    
        wp_dequeue_style($array);
        wp_deregister_style($array);
    
    }
    
    
    function script_dequeue_function()
    {
        global $wp_scripts;
        $array = array();
        // Runs through the queue scripts
        foreach ($wp_scripts->queue as $handle) :
            $array[] = $handle;
        endforeach;
    
        wp_dequeue_script($array);
        wp_dequeue_script($array);
    }
    
    
    add_action( 'wp_head', 'HideWpGeneratorAndScripts' );
    function HideWpGeneratorAndScripts()
    {
    
        $pageSlug = "Your Page Slug here.";
        $Template_Name = "Your Custom Template Name here.";
    
        if(is_page($pageSlug)) {
            add_action('wp_enqueue_scripts', 'style_dequeue_function');
            add_action('wp_enqueue_scripts', 'script_dequeue_function');
            remove_action('wp_head', 'wp_generator');
    
        }
        else if(is_page_template($Template_Name)) {
            add_action('wp_enqueue_scripts', 'style_dequeue_function');
            add_action('wp_enqueue_scripts', 'script_dequeue_function');
            remove_action('wp_head', 'wp_generator');
        }
    }
    

相关问题