astra主题中文免费版本代码优化

astra主题其实用免费版功能就足够,如果你不追求那些花里胡哨的前端展示。像我就一直在用免费版,不用担心使用其他免费主题存在的各种繁琐问题。同时,astra主题免费版更迎合国内用户的访问需求,简洁明了。

astra主题中文免费版本代码优化

本次提供的astra主题中文免费版本代码优化主要去除了一些非必要和影响网站收录的因素,主要是修改主题下function文件。

代码优化模板(将以下代码插入function文件中即可):

//去除头部多余加载信息
remove_action( 'wp_head', 'wp_generator' );//移除WordPress版本
remove_action( 'wp_head', 'rsd_link' );//移除离线编辑器开放接口
remove_action( 'wp_head', 'wlwmanifest_link' );//移除离线编辑器开放接口
remove_action( 'wp_head', 'index_rel_link' );//去除本页唯一链接信息
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_wp_head', 10, 0 );//清除前后文信息
remove_action( 'wp_head', 'feed_links', 2 );//移除文章和评论feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除分类等feed
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); //移除wp-json
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); //头部的JS代码
add_filter( 'show_admin_bar', '__return_false' );//移除wp-json链接
remove_action( 'wp_head', 'rel_canonical' ); //rel=canonical
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); //rel=shortlink
//remove_action( 'wp_head', 'wp_print_styles', 8 ); //移除后台插件加载css
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );//移除emoji载入js
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );//emoji载入js
remove_action( 'wp_print_styles', 'print_emoji_styles' );//移除emoji载入css
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action('wp_head','wp_resource_hints',2);//移除dns-prefetch

//文章内图片链接优化去除响应式长链接
function disable_srcset( $sources ) {
	return false;
}
add_filter( 'wp_calculate_image_srcset', 'disable_srcset' );

//去除图片高宽
add_filter( 'image_send_to_editor', 'fanly_remove_images_attribute', 10 );
function fanly_remove_images_attribute( $html ) {
    //$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
    $html = preg_replace( '/width="(\d*)"\s+height="(\d*)"\s+class="[^"]*"/', "", $html );
    $html = preg_replace( '/  /', "", $html );
    return $html;
}

//移除wp-json代码
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );

/**
 * 移除 WordPress 5.9以上版本 向前台页面添加的内联样式 css 和 svg 图像等多余代码  
 */
function lxtx_remove_all_global_styles(){
    remove_action('wp_enqueue_scripts', 'wp_enqueue_global_styles');
    remove_action('wp_footer', 'wp_enqueue_global_styles', 1);
    remove_action('wp_body_open', 'wp_global_styles_render_svg_filters');
}
add_action('after_setup_theme', 'lxtx_remove_all_global_styles', 10, 0);

// 移除头部 wp-json 标签和  header 中的shortlink(主要去除http头返回短连接和wp-json连接,需使用百度站长平台抓取诊断工具判断)
remove_action('wp_head','wp_shortlink_wp_head',10,0); 
remove_action('template_redirect','wp_shortlink_header',11,0);

// 移除头部 wp-json 标签和  header 中的 link(主要去除http头返回短连接和wp-json连接,需使用百度站长平台抓取诊断工具判断)
remove_action('wp_head', 'rest_output_link_wp_head', 10 );
remove_action('template_redirect', 'rest_output_link_header', 11 );

//移除aioseo_schema标记,此项如果你使用了all in one SEO插件可选择添加,如果未使用请自行删除。
add_filter( 'aioseo_schema_disable', 'aioseo_disable_schema' );

function aioseo_disable_schema( $disabled ) {
   return true;
}

上传覆盖之后刷新页面就行了,关于astra主题免费版本的代码优化教程到此结束。

本站资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。如有侵权请发送邮件至vizenaujmaslak9@hotmail.com删除。:FGJ博客 » astra主题中文免费版本代码优化

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址