WordPress优化篇—移除不需要的代码
Wodpress有我们许多用不到的功能代码,这些代码还有可能造成漏洞、拖慢博客,我们只需要在主题中添加一些代码,即可屏蔽这些对我们没有意义的代码。主要是优化网页头部的冗余代码,屏蔽文章Embed功能,禁用Rest api,移除emoji表情代码。
以下代码只需添加到主题的function.php文件中即可。代码均收集于网络,本人整理汇总。
WP精简头部代码
wp-head()函数在网页的头部引入了大量对我们没有多大意义的代码,根据注释选择你需要移除的代码。
- //去除头部冗余代码
- remove_action('wp_head', 'feed_links', 2 ); //移除文章和评论feed
- remove_action('wp_head', 'feed_links_extra', 3); //移除分类等feed
- remove_action('wp_head', 'rsd_link'); //移除离线编辑器开放接口
- remove_action('wp_head', 'wlwmanifest_link'); //移除离线编辑器开放接口
- remove_action('wp_head', 'index_rel_link'); //移除当前页面的索引
- remove_action('wp_head', 'start_post_rel_link', 10, 0); //移除最开始文章的url
- remove_action('wp_head', 'parent_post_rel_link', 10, 0 ); //移除后面文章的url
- remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); //移除相邻文章的url
- remove_action('wp_head', 'wp_generator'); //隐藏wordpress版本
- remove_action('wp_head', 'rel_canonical'); //去除默认canonical标签
- remove_action('wp_head','wp_shortlink_wp_head',10,0); //去除shortlink短链接
- remove_action('template_redirect','wp_shortlink_header',11,0); //去除shortlink短链接
移除emoji表情代码
wordpress4.2版本之后会自动在加载一段用于支持emjo表情的脚本(JS+CSS),但对于大多数人来说没有多大用,并且会拖慢博客速度,用下面的代码即可移除emoji表情代码。
- //移除emoji表情代码
- function disable_emojis() {
- remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
- remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
- remove_action( 'wp_print_styles', 'print_emoji_styles' );
- remove_action( 'admin_print_styles', 'print_emoji_styles' );
- remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
- remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
- remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
- add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
- }
- add_action( 'init', 'disable_emojis' );
- /**
- * Filter function used to remove the tinymce emoji plugin.
- *
- * @param array $plugins
- * @return array Difference betwen the two arrays
- */
- function disable_emojis_tinymce( $plugins ) {
- if ( is_array( $plugins ) ) {
- return array_diff( $plugins, array( 'wpemoji' ) );
- } else {
- return array();
- }
- }
屏蔽文章Embed功能
WordPress 4.4 增强了 Easy Embeds 功能,发布了一个新功能叫做 Post Embed,可以在任意 WordPress 站点用嵌入的方式插入 WordPress 文章。如果觉得这玩意对你没什么意义,你也可以屏蔽它。
- remove_action('rest_api_init', 'wp_oembed_register_route');
- remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
- remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
- remove_filter('oembed_response_data', 'get_oembed_response_data_rich', 10, 4);
- remove_action('wp_head', 'wp_oembed_add_discovery_links');
- remove_action('wp_head', 'wp_oembed_add_host_js');
禁用REST API
WordPress4.4开始增加REST API功能, 对于一般的网站是没有需要的,反而会拖累网站的速度,所以我们尽可能的禁止掉这些不必要的功能需求。
- // 屏蔽REST API
- add_filter('json_enabled', '__return_false' );
- add_filter('json_jsonp_enabled', '__return_false' );
- add_filter('rest_enabled', '__return_false');
- add_filter('rest_jsonp_enabled', '__return_false');
- // 移除头部wp-json标签和HTTP header中的link
- remove_action('wp_head', 'rest_output_link_wp_head', 10 );
- remove_action('template_redirect', 'rest_output_link_header', 11 );
禁止英文半角转换成中文全角
禁止字符转义的所有代码如下,你可以根据自己的需要,选自己要的代码,添加到主题的 functions.php 文件:
- // 取消转义 Quotmarks Replacer
- $qmr_work_tags = array(
- 'the_title', // http://codex.wordpress.org/Function_Reference/the_title
- 'the_content', // http://codex.wordpress.org/Function_Reference/the_content
- 'the_excerpt', // http://codex.wordpress.org/Function_Reference/the_excerpt
- // 'list_cats', Deprecated. http://codex.wordpress.org/Function_Reference/list_cats
- 'single_post_title', // http://codex.wordpress.org/Function_Reference/single_post_title
- 'comment_author', // http://codex.wordpress.org/Function_Reference/comment_author
- 'comment_text', // http://codex.wordpress.org/Function_Reference/comment_text
- // 'link_name', Deprecated.
- // 'link_notes', Deprecated.
- 'link_description', // Deprecated, but still widely used.
- 'bloginfo', // http://codex.wordpress.org/Function_Reference/bloginfo
- 'wp_title', // http://codex.wordpress.org/Function_Reference/wp_title
- 'term_description', // http://codex.wordpress.org/Function_Reference/term_description
- 'category_description', // http://codex.wordpress.org/Function_Reference/category_description
- 'widget_title', // Used by all widgets in themes
- 'widget_text' // Used by all widgets in themes
- );
- foreach ( $qmr_work_tags as $qmr_work_tag ) {
- remove_filter ($qmr_work_tag, 'wptexturize');
- }
还可以使用下面的形式:
- //取消内容转义
- remove_filter('the_content', 'wptexturize');
- //取消摘要转义
- remove_filter('the_excerpt', 'wptexturize');
- //取消评论转义
- remove_filter('comment_text', 'wptexturize');
发表评论
要发表评论,您必须先登录。