17713433920 info@mac163.com

对于习惯了国内CMS系统的站长来说,使用国外的wordpress系统,感觉代码两个字慢与乱,以下是最模板总结搜集的优化代码,请写在模板文件底部functions.php。

  /*
  * 图片添加alt属性
  */
function img_alt( $imgalt ){
        global $post;
        $title = $post->post_title;
        $imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\[^>]*>";
        if(preg_match_all("/$imgUrl/siU",$imgalt,$matches,PREG_SET_ORDER)){
                if( !empty($matches) ){
                        for ($i=0; $i < count($matches); $i++){
                                $tag = $url = $matches[$i][0];
                                $judge = '/alt=/';
                                preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
                                if( count($match) < 1 ) $altURL = ' alt="'.$title.'" '; $url = rtrim($url,'>');
                                $url .= $altURL.'>';
                                $imgalt = str_replace($tag,$url,$imgalt);
                        }
                }
        }
        return $imgalt;
}
 
add_filter( 'the_content','img_alt');
 
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
 
function change_attachement_image_attributes( $attr, $attachment ){
    // Get post parent
    $parent = get_post_field( 'post_parent', $attachment);
 
    // Get post type to check if it's product
    $type = get_post_field( 'post_type', $parent);
    if( $type != 'product' ){
        return $attr;
    }
 
    /// Get title
    $title = get_post_field( 'post_title', $parent);
 
    $attr['alt'] = $title;
    $attr['title'] = $title;
 
    return $attr;
}
 
 
 
function remove_open_sans() {   
    wp_deregister_style( 'open-sans' );   
    wp_register_style( 'open-sans', false );   
    wp_enqueue_style('open-sans','');   
}   
add_action( 'init', 'remove_open_sans' );
//去除头部冗余代码   
    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 功能
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_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' );
 
 
 
 
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('template_redirect', 'rest_output_link_header', 11, 0);
 
function remove_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
return array_diff( wp_dependencies_unique_hosts(), $hints );
}
return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );
最全优化wordpress几个代码搜集总结
最全优化wordpress几个代码搜集总结

微信二维码

微信扫描二维码联系我们!
我们在微信上24小时期待你的声音
提供外贸路由器设备产品,轻松翻墙,解答:WP主题推荐,WP网站建设,Google SEO,百度SEO,专业服务器环境搭建等!


需要提供WordPress主题/插件的汉化服务可以随时联系我们!另外成品WordPress网站以及半成品WordPress网站建设,海外Google SEO优化托管服务,百度SEO优化托管服务,Centos/Debian服务器WP专用环境搭建,WP缓存服务器搭建,我们都是你的首选,拥有多年WP开源程序服务经验,我们一直在坚持客户体验,没有最好,只有更好!
回到顶部