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

时间:2015-08-06 05:42:16

标签: php wordpress

我有一个博客(使用 <filter id = "i1" x="-40%" y="-40%" width="180%" height="180%"> 子主题)和一个页面,我必须禁用WordPress添加的所有样式,脚本和元标记,但添加的元标记除外通过 All in One Seo Pack插件。

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

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

1 个答案:

答案 0 :(得分:3)

有两个功能

  1. style_dequeue_function()它会删除所有样式。
  2. script_dequeue_function()它将删除所有脚本。
  3. 您需要提供页面标题或模板名称。

      

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

      

    $Template_Name按模板名称

    remove_action将删除WordPress生成器代码。

    remove_action('wp_head', 'wp_generator');
    
      

    注意:只有deque样式或WP enque函数添加的脚本。

    例如:

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