对于习惯了国内CMS系统的站长来说,使用国外的wordpress系统,感觉代码两个字慢与乱,以下是好模板总结搜集的优化代码,请写在模板文件底部functions.php。
上一篇:使用WordPress作为一个静态网站生成器的疯狂速度,可靠性和安全性
下一篇:更改Woocommerce相关产品的数量
/*
* 图片添加alt属性
*/
function img_alt( $imgalt ){
global $post;
$title = $post->post_title;
$imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
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 );