本站使用sahifa主题,如果你也是使用的sahifa主题 可以把以下内容直接复制到主题functions.php文件,直接使用
1、给文章图片自动添加alt信息
function image_alt($content) {
global $post;
$title = $post->post_title;
$src = array('/src="(.+?.(jpg|bmp|png|jepg|gif))"/i' => 'src="$1" alt="'.$title.'"');
foreach($src as $pattern => $replacement){
$content = preg_replace($pattern,$replacement,$content);
}
return $content;
}
add_filter( 'the_content', 'image_alt' );
2、屏蔽后台页脚WordPress版本信息
function change_footer_admin () {return '';}
add_filter('admin_footer_text', 'change_footer_admin', 9999);
function change_footer_version() {return '';}
add_filter( 'update_footer', 'change_footer_version', 9999);
3、压缩html代码
function wp_compress_html(){
function wp_compress_html_main ($buffer){
$initial=strlen($buffer);
$buffer=explode("", $buffer);
$count=count ($buffer);
for ($i = 0; $i <= $count; $i++){
if (stristr($buffer[$i], '')) {
$buffer[$i]=(str_replace("", " ", $buffer[$i]));
} else {
$buffer[$i]=(str_replace("\t", " ", $buffer[$i]));
$buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i]));
$buffer[$i]=(str_replace("\n", "", $buffer[$i]));
$buffer[$i]=(str_replace("\r", "", $buffer[$i]));
while (stristr($buffer[$i], ' ')) {
$buffer[$i]=(str_replace(" ", " ", $buffer[$i]));
}
}
$buffer_out.=$buffer[$i];
}
$final=strlen($buffer_out);
$savings=($initial-$final)/$initial*100;
$savings=round($savings, 2);
//$buffer_out.="\n";
return $buffer_out;
}
ob_start("wp_compress_html_main");
}
add_action('get_header', 'wp_compress_html');
4、精简WordPress前后台顶部工具栏
function my_edit_toolbar($wp_toolbar) {
$wp_toolbar->remove_node('wp-logo'); //去掉Wordpress LOGO
// $wp_toolbar->remove_node('site-name'); //去掉网站名称
$wp_toolbar->remove_node('updates'); //去掉更新提醒
$wp_toolbar->remove_node('comments'); //去掉评论提醒
$wp_toolbar->remove_node('new-content'); //去掉新建文件
// $wp_toolbar->remove_node('top-secondary'); //用户信息
}
add_action('admin_bar_menu', 'my_edit_toolbar', 999);
5、启用友情链接
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
6、移除登录页面标题中的“ — WordPress”
add_filter('login_title', 'fanly_remove_login_title', 10, 2);
function fanly_remove_login_title($login_title, $title){
return $title.' ‹ '.get_bloginfo('name');
}
7、移除后台页面标题中的“ — WordPress”
add_filter('admin_title', 'fanly_remove_admin_title', 10, 2);
function fanly_remove_admin_title($admin_title, $title){
return $title.' ‹ '.get_bloginfo('name');
}
8、禁止后台加载谷歌字体
function wp_remove_open_sans_from_wp_core() {
wp_deregister_style( 'open-sans' );
wp_register_style( 'open-sans', false );
wp_enqueue_style('open-sans','');
}
add_action( 'init', 'wp_remove_open_sans_from_wp_core' );
9、更改后台字体为雅黑
function Fanly_admin_lettering() {
echo '<style type="text/css">
* { font-family: "Microsoft YaHei"; }
#activity-widget #the-comment-list .avatar { max-width: 50px; max-height: 50px; }
</style>';
}
add_action( 'admin_head', 'Fanly_admin_lettering' );
10、禁用文章自动保存
add_action('wp_print_scripts','disable_autosave');
function disable_autosave(){
wp_deregister_script('autosave');
}
11、禁用文章修订版本
add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 );
function specs_wp_revisions_to_keep( $num, $post ) {
return 0;
}
12、禁止WordPress头部加载s.w.org
add_filter( 'emoji_svg_url', '__return_false' );
13、禁用emoji
function disable_emojis() {
remove_action( 'wp_print_styles', 'print_emoji_styles' );
}
add_action( 'init', 'disable_emojis' );
14、清除HEAD无用信息
function ashuwp_clean_theme_meta(){
remove_action( 'wp_head', 'print_emoji_detection_script', 7, 1);
remove_action( 'wp_print_styles', 'print_emoji_styles', 10, 1);
remove_action( 'wp_head', 'rsd_link', 10, 1);
remove_action( 'wp_head', 'wp_generator', 10, 1);
remove_action( 'wp_head', 'feed_links', 2, 1);
remove_action( 'wp_head', 'feed_links_extra', 3, 1);
remove_action( 'wp_head', 'index_rel_link', 10, 1);
remove_action( 'wp_head', 'wlwmanifest_link', 10, 1);
remove_action( 'wp_head', 'start_post_rel_link', 10, 1);
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0);
remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0);
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action( 'wp_head', 'rest_output_link_wp_head', 10, 0);
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10, 1);
remove_action( 'wp_head', 'rel_canonical', 10, 0);
remove_action( 'wp_head', 'wp_resource_hints', 2, 0);
}
add_action( 'after_setup_theme', 'ashuwp_clean_theme_meta' ); //清除wp_head带入的meta标签
15、移除WordPress头部加载DNS预获取(dns-prefetch)
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 );
16、移除评论人名字的链接
function disable_comment_author_links( $author_link ){
return strip_tags( $author_link );}
add_filter( 'get_comment_author_link', 'disable_comment_author_links' );
17、禁止评论超链接
remove_filter('comment_text', 'make_clickable', 9);
18、移除Feed
function wp_disable_feed() {
wp_die(__('<h1>本博客不提供 Feed,请访问网站<a href="'.get_bloginfo('url').'">首页</a>!</h1>'));}
add_action('do_feed', 'wp_disable_feed', 1);
add_action('do_feed_rdf', 'wp_disable_feed', 1);
add_action('do_feed_rss', 'wp_disable_feed', 1);
add_action('do_feed_rss2', 'wp_disable_feed', 1);
add_action('do_feed_atom', 'wp_disable_feed', 1);
19、关闭了traceback和pingback
add_filter( 'xmlrpc_methods', 'remove_xmlrpc_pingback_ping' );
function remove_xmlrpc_pingback_ping( $methods ) {
unset( $methods['pingback.ping'] );
return $methods;}
add_action ( 'pre_ping', 'no_self_ping' );
20、禁止WordPress自动更新邮件通知
add_filter( 'auto_core_update_send_email', 'wpb_stop_auto_update_emails', 10, 4 );
function wpb_stop_update_emails( $send, $type, $core_update, $result ) {
if ( ! emptyempty( $type ) && $type == 'success' ) {
return false;
}
return true;
}
21、WordPress上传文件自动重命名
function git_upload_filter($file) {
$time = date("YmdHis");
$file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
return $file;
}
add_filter('wp_handle_upload_prefilter', 'git_upload_filter');
22、禁止冒用管理员发评论
function usercheck($incoming_comment) {
$isSpam = 0;
if (trim($incoming_comment['comment_author']) == ''.get_option('admin_name').'')
$isSpam = 1;
if (trim($incoming_comment['comment_author_email']) == ''.get_option('admin_email').'')
$isSpam = 1;
if(!$isSpam)
return $incoming_comment;
err('<i class="fa fa-exclamation-circle"></i>请勿冒充管理员发表评论!');
}
23、添加文章版权信息
function copyright($content) {
if(is_single()||is_feed()) {
$content.='<div style="margin-top: 10px; font-style: italic;border: 1px dashed #ccc;">原创文章,转载请注明: 转载自<a href="'.get_bloginfo(url).'">'.get_bloginfo(name).'</a><br>本文链接地址: <a href="'.get_permalink().'">'.get_the_title().'</a></div>';
}
return $content;
}
add_filter ('the_content', 'copyright');
样式在本站自己复制吧
24、使用smtp发邮件
function mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用SMTPAuth服务
$phpmailer->Port = 465;//MTP邮件发送端口,这个和下面的对应,如果这里填写25,则下面为空白
$phpmailer->SMTPSecure ="ssl";//是否验证 ssl,这个和上面的对应,如果不填写,则上面的端口须为25
$phpmailer->Host = "smtp.exmail.qq.com";//邮箱的SMTP服务器地址,如果是QQ的则为:smtp.exmail.qq.com
$phpmailer->Username = "admin@liry.cn";//你的邮箱地址
$phpmailer->Password ="**********";//你的邮箱登陆密码
}
add_action('phpmailer_init', 'mail_smtp');
//下面这个很重要,得将发件地址改成和上面smtp邮箱一致才行。
function ashuwp_wp_mail_from( $original_email_address ) {
return 'admin@liry.cn';
}
add_filter( 'wp_mail_from', 'ashuwp_wp_mail_from' );
25、启用缓存压缩插件
WP Super Cache和Merge Minify Refresh以及头像缓存插件NIX Gravatar Cache
26、去除sahifa主题前端谷歌字体
打开 \framework\functions\common-scripts.php 165行
elseif( $font_type == 'early-google'){
$font_name = str_replace (" ","", $font_pieces[0] );
$protocol = is_ssl() ? 'https' : 'http';
wp_enqueue_style( $font_name , $protocol.'://fonts.googleapis.com/earlyaccess/'.$font_name);</code>
}else{
$font_name = str_replace (" ","+", $font_pieces[0] );
$font_variants = str_replace ("|",",", $font_pieces[1] );
$protocol = is_ssl() ? 'https' : 'http';
wp_enqueue_style( $font_name , $protocol.'://fonts.googleapis.com/css?family='.$font_name . ':' . $font_variants.$char_set );
}
把这几行注释即可
本文链接地址: 分享Sahifa主题优化记录