zoukankan      html  css  js  c++  java
  • WordPress对header的清洗 :删除无用的标记和链接

    [

    我们知道,wp_head();会引入很多我们不想要的资源来到header里面,增加网站的负载,而对于wordpress程序,虽然功能很多,但是还是要尽量给网站减负,这里对删除wordpress里面header很多没用的标记和元素。这些东西包括profile, pingback, alternate, EditURI, wlwmainfest, prev, next, generator meta, shortlink, index, start等。这些标记不仅会对增加页面的体积,而且可能导致安全问题。比如“generator” 元标记就会泄露Wordpress的版本号,如果你没用及时更新一个已经曝出有安全漏洞的版本。

    WordPress移除header很多没用的标记和元素

    在functions.php加入下面的代码即可,代码后面的注释,可根据自己的需要加入:

    //洗header
    remove_action( 'wp_head', 'feed_links', 2 ); //去除文章feed
    remove_action( 'wp_head', 'rsd_link' ); //针对Blog的远程离线编辑器接口
    remove_action( 'wp_head', 'wlwmanifest_link' ); //Windows Live Writer接口
    remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); //移除后面文章的url
    remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); //移除最开始文章的url
    remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );//自动生成的短链接
    remove_action( 'wp_head', 'wp_generator' ); // 移除版本号
    remove_action('wp_head', 'index_rel_link');//当前文章的索引
    remove_action('wp_head', 'feed_links_extra', 3);// 额外的feed,例如category, tag页
    remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // 上、下篇.
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//rel=pre
    wp_deregister_script('l10n');
    remove_filter('the_content', 'wptexturize');//禁用半角符号自动转换为全角
    remove_action('wp_head', 'wp_resource_hints', 2);//禁用类似rel='dns-prefetch' href='//fonts.googleapis.com'
    
    //移除wp-json
    add_filter('json_enabled', '__return_false' );
    add_filter('json_jsonp_enabled', '__return_false' );
    
    remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
    
    /**
     * 移除 WordPress 加载的JS和CSS链接中的版本号
     * https://www.wpdaxue.com/remove-js-css-version.html
     */
    function wpdaxue_remove_cssjs_ver( $src ) {
    	if( strpos( $src, 'ver=' ) )
    		$src = remove_query_arg( 'ver', $src );
    	return $src;
    }
    add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
    add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );

    再附赠停止后台更新提示的代码,还是在functions.php中加入:

    //关闭所有更新提示
    add_filter('pre_site_transient_update_core', create_function('$a', "return null;")); // 关闭核心提示
    add_filter('pre_site_transient_update_plugins', create_function('$a', "return null;")); // 关闭插件提示
    add_filter('pre_site_transient_update_themes',  create_function('$a', "return null;")); // 关闭主题提示
    remove_action('admin_init', '_maybe_update_core');    // 禁止 WordPress 检查更新
    remove_action('admin_init', '_maybe_update_plugins'); // 禁止 WordPress 更新插件
    remove_action('admin_init', '_maybe_update_themes');  // 禁止 WordPress 更新主题

    ]
    转载请保留页面地址:https://www.breakyizhan.com/wpress/6795.html
  • 相关阅读:
    数据类面试题
    java二进制文件复制
    String源码
    集合类题目
    OBJ-C
    java文件(文件夹)操作
    java中输入方式Scanner和BufferedReader
    二次分发举例
    Eclipse常用快捷键
    c#获取新浪微博登录cookie
  • 原文地址:https://www.cnblogs.com/breakyizhan/p/13287107.html
Copyright © 2011-2022 走看看