博客重建之后,一直在寻找适合自己的一个主题,从开始的WordPress默认2019主题,到知更鸟最新版等等,尝试了四五个主题后,还是发现官方12年默认的主题Twenty Twelve比较适合我,于是稍加修改,决定用此主题了。纯代码,没有图片等累赘,简单,便捷,好用,唯一的缺点就是字有点小。

下面说说改进的地方吧,
- 修改 主题页脚 (footer.php)文件,去掉了啰嗦的自豪的采用wordpress,添加了版权信息及网站地图、修改边栏功能中WordPress版权信息!
- 安装了几款必备插件,Autoptimize、Google Sitemap by BestWebSoft、WP Mail SMTP、
WP Super Cache - 修改了模版,评论回复邮件通知,审核通过通知。
- 修改了允许中文注册,重置密码连接无效等问题。
- 修改title,增加seo关键词及描述。
- 修改字体大小。
- 修改禁止缩微图功能,避免图片默认被切了N张占用空间。
- 增加标签内部连接。
- 禁止自动保存和历史版本,优化数据库速度。
一、.修改主题页脚 (footer.php)文件,去掉,自豪的采用wordpress、位置:/wp-content/themes/twentytwelve/footer.php,原有代码21行位置,如下:
<a href="<?php echo esc_url( __( 'https://wordpress.org/', 'twentytwelve' ) ); ?>" class="imprint" title="<?php esc_attr_e( 'Semantic Personal Publishing Platform', 'twentytwelve' ); ?>">
<?php printf( __( 'Proudly powered by %s', 'twentytwelve' ), 'WordPress' ); ?>
</a>
修改成如下代码:(这里是本站代码,可以根据自己所需修改)
Copyright © 2011 - 2018 <a href="<?php echo get_option(‘home’); ?>" title="<?php bloginfo(‘name’); ?>" rel="generator">SheRuo.com</a>. All Rights Reserved
<a href="https://sheruo.com/sitemap.xml" style="float:right" target="_blank" title="站点地图">Sitemap</a>
去掉功能处wordpress.org最新办法,位于wp-includes/widgets中的 class-wp-widget-meta.php ,找到如下代码删除或//注释掉即可!
<?php
/**
* Filter the "Powered by WordPress" text in the Meta widget.
* @since 3.6.0
* @param string $title_text Default title text for the WordPress.org link.
*/
echo apply_filters( 'widget_meta_poweredby', sprintf( '<li><a href="%s" title="%s">%s</a></li>',
esc_url( __( 'https://wordpress.org/' ) ),
esc_attr__( 'Powered by WordPress, state-of-the-art semantic personal publishing platform.' ),
_x( 'WordPress.org', 'meta widget link text' )
) );
wp_meta();
?>
二、安装了几款必备插件,Autoptimize、Google Sitemap by BestWebSoft、WP Mail SMTP、
WP Super Cache,这里就不单独一一介绍了,后期如果遇到设置问题,可以留言给我。
三、修改模版,设置评论回复通知功能,代码如下,加入在模板函数 (functions.php)下即可。下面是本站代码,可根据自己要求修改。
/**
* WordPress 评论通过审核后邮件通知评论人
* https://sheruo.com/50.html
*/
add_action('comment_unapproved_to_approved', 'wpdx_comment_approved');
function wpdx_comment_approved($comment){
if (is_email($comment->comment_author_email)){
$post_link = get_permalink($comment->comment_post_ID);
$title = '您在【' . get_bloginfo('name') . '】的评论已通过审核';
$body = '您在《<a href="' . $post_link . '" target="_blank" >' . get_the_title($comment->comment_post_ID) . '</a>》中发表的评论已通过审核!<br /><br />';
$body .= '<strong>您的评论:</strong><br />';
$body .= strip_tags($comment->comment_content) . '<br /><br />';
$body .= '您可以:<a href="' . get_comment_link($comment->comment_ID) . '" target="_blank">查看您的评论</a> | <a href="' . $post_link . '#comments" target="_blank">查看其他评论</a> | <a href="' . $post_link . '" target="_blank">再次阅读文章</a><br /><br />';
$body .= '欢迎再次光临【<a href="' . get_bloginfo('url') . '" target="_blank" title="' . get_bloginfo('description') . '">' . get_bloginfo('name') . '</a>】。';
$body .= '<br /><br />注:此邮件为系统自动发送,请勿直接回复';
@wp_mail($comment->comment_author_email, $title, $body, "Content-Type: text/html; charset=UTF-8");
}
}
/**
* WordPress 评论回复邮件
* https://sheruo.com/50.html
*/
add_action('comment_post','CommentsReplyNotification');
function CommentsReplyNotification($comment_id){
//取得插入评论的id
$c = get_comment($comment_id);
//取得评论的父级id
$comment_parent = $c->comment_parent;
//取得评论的内容
$c_content = $c->comment_content;
//评论者email
$c_author_email = $c->comment_author_email;
if($comment_parent != 0){
$pc = get_comment($comment_parent);
$comment_ID = $pc->comment_ID;
$comment_author = $pc->comment_author;
$comment_author_email = $pc->comment_author_email;
$comment_post_ID = $pc->comment_post_ID;
$comment_content = $pc->comment_content;
$ps = get_post($comment_post_ID);
$author_id = $ps->post_author;
$u_email = get_user_meta($author_id,'email',true);
//判断自己的回复,如果自己参与评论,不给自己发送邮件通知
if($c_author_email == $comment_author_email || $comment_author_email == $u_email ){
return;
}
$post_title = $ps->post_title;
$link = get_permalink($comment_post_ID);
//邮件内容,可以自定义内容
$content = "尊敬的".$comment_author."您好,你发布于SheRuo.com\" ".$post_title."\"文章的评论:\r\n".$comment_content."\r\n有了新回复:\r\n".$c_content."\r\n点击链接回复评论:".$link."#comment-".$comment_ID;
//发送邮件
wp_mail($comment_author_email,'评论回复:'.$post_title, $content);
}
}
四,允许中文注册的修改,在模板函数 (functions.php)文件,添加如下代码即可。
/**
* 支持中文注册
* https://www.jiaxunip.com
*/
function ludou_non_strict_login( $username, $raw_username, $strict ) {
if( !$strict )
return $username;
return sanitize_user(stripslashes($raw_username), false);
}
add_filter('sanitize_user', 'ludou_non_strict_login', 10, 3);
修改wordpress重置密码连接无效,函数修改办法模板函数 (functions.php)文件,添加如下代码
/**
* 修复WordPress找回密码提示“抱歉,该key似乎无效”问题
*/
function reset_password_message( $message, $key ) {
if ( strpos($_POST['user_login'], '@') ) {
$user_data = get_user_by('email', trim($_POST['user_login']));
} else {
$login = trim($_POST['user_login']);
$user_data = get_user_by('login', $login);
}
$user_login = $user_data->user_login;
$msg = __('有人要求重设如下帐号的密码:'). "\r\n\r\n";
$msg .= network_site_url() . "\r\n\r\n";
$msg .= sprintf(__('用户名:%s'), $user_login) . "\r\n\r\n";
$msg .= __('若这不是您本人要求的,请忽略本邮件,一切如常。') . "\r\n\r\n";
$msg .= __('要重置您的密码,请打开下面的链接:'). "\r\n\r\n";
$msg .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') ;
return $msg;
}
add_filter('retrieve_password_message', reset_password_message, null, 2);
五、修改title,增加seo关键词及描述。这个需要修改 主题页眉 (header.php)、在/title后加入如下代码,单独页面获取简要描述,标签作为关键词。
<?php
$description = '';
$keywords = '';
if (is_home() || is_page()) {
// 将以下引号中的内容改成你的主页description
$description = "SheRuo.com、一个专注知识产权与经验分享的博客";
// 将以下引号中的内容改成你的主页keywords
$keywords = "sheruo, 知识产权博客, 商标博客, 专利博客";
}
elseif (is_single()) {
$description1 = get_post_meta($post->ID, "description", true);
$description2 = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
// 填写自定义字段description时显示自定义字段的内容,否则使用文章内容前200字作为描述
$description = $description1 ? $description1 : $description2;
// 填写自定义字段keywords时显示自定义字段的内容,否则使用文章tags作为关键词
$keywords = get_post_meta($post->ID, "keywords", true);
if($keywords == '') {
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$keywords = $keywords . $tag->name . ", ";
}
$keywords = rtrim($keywords, ', ');
}
}
elseif (is_category()) {
// 分类的description可以到后台 - 文章 -分类目录,修改分类的描述
$description = category_description();
$keywords = single_cat_title('', false);
}
elseif (is_tag()){
// 标签的description可以到后台 - 文章 - 标签,修改标签的描述
$description = tag_description();
$keywords = single_tag_title('', false);
}
$description = trim(strip_tags($description));
$keywords = trim(strip_tags($keywords));
?>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
六、修改字体大小,这个重点说下,这个具体去改比较麻烦,这里只说下简单修改全局的办法。修改全局大小位置在,样式表 (style.css),480行左右。默认是font-size: 87.5%; font-size: 14px; 详细的对照表如下,可以根据自己要求修改、

七,禁止缩微图功能,历史版本,自动保存,文章内容标签添加连接,都是模板函数 (functions.php)文件添加函数,代码如下
/* 自动添加内链
/* -------------------------------- */
$match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接
$match_num_to = 1; //一篇文章中同一个标签最多自动链接几次
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
function tag_link($content){
global $match_num_from,$match_num_to;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "tag_sort");
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
$cleankeyword = stripslashes($keyword);
$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('%s'))."\"";
$url .= ' target="_blank"';
$url .= ">".addcslashes($cleankeyword, '$')."</a>";
$limit = rand($match_num_from,$match_num_to);
$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$cleankeyword = preg_quote($cleankeyword,'\'');
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
}
}
return $content;
}
add_filter('the_content','tag_link',1);
//禁用自动保存
add_action('wp_print_scripts', 'fanly_no_autosave');
function fanly_no_autosave() { wp_deregister_script('autosave'); }
//禁用所有文章类型的修订版本
add_filter( 'wp_revisions_to_keep', 'fanly_wp_revisions_to_keep', 10, 2 );
function fanly_wp_revisions_to_keep( $num, $post ) { return 0;}
//彻底禁止WordPress缩略图
add_filter( 'add_image_size', create_function( '', 'return 1;' ) );
ok,基本上修改了这些,还有些没整理。编辑文章时发现Twenty Twelve已经出了新版,赶快去更新了,然后看看有没有折腾的空间。
感谢,终于找到修改字体的方法了
djghjh好