# 处理部分WordPress核心代码或功能，让你的网站更快

- Link: https://yjyj.net/learn/wordpress-learn/4669.html
- Published: 2014-06-23T19:25:29+08:00
- Author: 小 虾

⚡ 生猛大蝦编辑部 · 30 秒核心看点

 * 用钩子移除后台与前台冗余输出，减少无用请求和页面噪音。
 * 精简默认小工具、仪表盘模块，能让 WordPress 后台更清爽。
 * 核心结论：不是盲目删功能，而是按需裁掉默认负担，提升速度与可维护性。

[Open sans 的谷歌字体链接](https://yjyj.net/learn/wordpress-learn/4662.html)

admin bar左侧的wordpress logo和链接等

[⌊tenpay1501⌉⌊tenpay1501⌉[

就是上图最左侧的Logo及其子菜单及其链接什么的。

    ```decode:true
    add_action( 'admin_bar_menu', 'cwp_remove_wp_logo_from_admin_bar_new', 25 );
    function cwp_remove_wp_logo_from_admin_bar_new( $wp_admin_bar ) {
        $wp_admin_bar->remove_node( 'wp-logo' );
    }
    ```

加载的css和js后面的版本号的问题

    ```decode:true
    if(!function_exists('cwp_remove_script_version')){
        function cwp_remove_script_version( $src ){  return remove_query_arg( 'ver', $src ); }
        add_filter( 'script_loader_src', 'cwp_remove_script_version' );
        add_filter( 'style_loader_src', 'cwp_remove_script_version' );
    }
    ```

WP前台顶部清理

    ```decode:true
    function cwp_header_clean_up(){
        if (!is_admin()) {
            foreach(array('wp_generator','rsd_link','index_rel_link','start_post_rel_link','wlwmanifest_link') as $clean){remove_action('wp_head',$clean);}
            remove_action( 'wp_head', 'feed_links_extra', 3 );
            remove_action( 'wp_head', 'feed_links', 2 );
            remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
            remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
            remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 );
            foreach(array('single_post_title','bloginfo','wp_title','category_description','list_cats','comment_author','comment_text','the_title','the_content','the_excerpt') as $where){
             remove_filter ($where, 'wptexturize');
            }
            /*remove_filter( 'the_content', 'wpautop' );
            remove_filter( 'the_excerpt', 'wpautop' );*/
            wp_deregister_script( 'l10n' );
        }
    }
    ```

移除某些WP自带的小工具

    ```decode:true
    function coolwp_remove_meta_widget() {
         unregister_widget('WP_Widget_Pages');
         unregister_widget('WP_Widget_Calendar');
         //unregister_widget('WP_Widget_Archives');
         unregister_widget('WP_Widget_Links');
         unregister_widget('WP_Widget_Meta');
        // unregister_widget('WP_Widget_Search');
          // unregister_widget('WP_Widget_Text');
         unregister_widget('WP_Widget_Categories');
         unregister_widget('WP_Widget_Recent_Posts');
         unregister_widget('WP_Widget_Recent_Comments');
         unregister_widget('WP_Widget_RSS');
         unregister_widget('WP_Widget_Tag_Cloud');
         unregister_widget('WP_Nav_Menu_Widget');
        /*register my custom widget*/
        register_widget('WP_Widget_Meta_Mod');
    }
    add_action( 'widgets_init', 'coolwp_remove_meta_widget',11 );
    ```

移除WP为仪表盘(dashboard)页面加载的小工具,放心大胆的全部移除吧

    ```decode:true
    function cwp_remove_dashboard_widgets() {
        global $wp_meta_boxes;
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts']);
        unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
        unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
    }
    add_action('wp_dashboard_setup', 'cwp_remove_dashboard_widgets',11 );
    ```

加载自定义jQuery库的链接

    ```decode:true
    /*Using SinaApp jQuery CDN*/
    function cwp_modify_jquery() {
        if (!is_admin()) {
            wp_deregister_script('jquery');
            wp_register_script('jquery', 'http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js', false, '1.9.1');
            wp_enqueue_script('jquery');
        }
    }
    add_action('init', 'cwp_modify_jquery');
    ```

❓ 延伸问答与排错指南（FAQ）

WordPress 怎么移除后台顶部 Logo？

可以通过 admin_bar_menu 钩子，在合适优先级里调用 remove_node('wp-logo')，把后台顶
部左侧的 WordPress Logo 和相关菜单去掉。这样能让管理栏更简洁，也适合做定制化后台
界面。

为什么要清理 WordPress 头部冗余代码？

WordPress 默认会在前台 head 里输出一些版本、链接和兼容性相关内容。清理这些冗余项
后，页面头部更干净，减少不必要的输出，也有助于降低暴露信息和提升维护体验。

能不能直接删掉所有默认小工具和仪表盘模块？

可以按需移除，但建议先确认站点是否真的不用这些功能。文章思路是通过 unregister_widget
和 unset 等方式精简默认模块，让后台更轻、更专注；如果某些小工具还在用，就不要一刀
切。

📚 【[WordPress 学习](https://yjyj.net/category/learn/wordpress-learn)】专栏延伸
阅读与热门精选（[全站文章归档](https://yjyj.net/archive)）：

 * [禁用 Google Fonts，加快 WordPress 后台打开速度](https://yjyj.net/learn/wordpress-learn/4662.html)
 * [wordpress出现500错误肿么办!!!](https://yjyj.net/learn/wordpress-learn/4659.html)
 * [再撸wordpress后台菜单](https://yjyj.net/learn/wordpress-learn/4684.html)
 * [关闭google fonts补全](https://yjyj.net/learn/wordpress-learn/4722.html)
 * 热门必读[css布局实例](https://yjyj.net/learn/3901.html)
 * 热门必读[程序员漫游指南](https://yjyj.net/what/5830.html)
