WordPress 中的 functions.php 文件可以对网站添加新功能或特性。在该文件中编写 PHP 代码,用来自定义函数或调用现有的 WordPress 函数。
functions.php 文件一般位于所使用主题的目录下,对 functions.php 文件进行修改,只会影响当前所使用的主题。
一般 functions.php 文件地址位于如下:
wp-content/themes/当前主题
在 functions.php 文件中编写代码实现特定的功能比较方便,编写相关代码后,如果出现错误或者没有达到预期的效果,然后进行删除即可。
思意博客博客整理了几段适合放置在 functions.php 文件的代码,功能比较实用,经测试有效,现在整理出来。
一、图片自动添加ALT和TITLE
/** 图片自动添加ALT和TITLE */
function image_alt_title($content){
global $post;preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images)) {foreach($images[1] as $index => $value)
{
$new_img = str_replace('<img', '<img alt="'.get_the_title().'-'.get_bloginfo('name').'"'.'title="'.get_the_title().'-'.get_bloginfo('name').'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);}}
return $content;
}
add_filter('the_content', 'image_alt_title', 99999);
二、隐藏后台登录地址
//隐藏wordpress后台登录地址
add_action('login_enqueue_scripts','login_protection');
function login_protection(){
if($_GET['www'] != 'youyuela' || $_GET['dot'] != 'com'){
header("HTTP/1.1 404 Not Found");
exit();
}
}
其中,代码第3行中的 www、youyuela、dot 和 com 可以自定义替换为自己想要的。
这样,后台登录地址就变成如下:
/wp-login.php?www=youyuela&dot=com
若地址错误将返回404信息。
隐藏了后台登录地址,多多少少也能增强一些安全性。
三、防止部分外文评论
//屏蔽外文评论
function syz_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
$jpattern ='/[ぁ-ん]+|[ァ-ヴ]+/u';
$ruattern ='/[А-я]+/u';
$arattern ='/[؟-ض]+|[ط-ل]+|[م-م]+/u';
$thattern ='/[ก-๛]+/u';
if(preg_match($jpattern, $incoming_comment['comment_content'])){
wp_die( "Japanese Get out!日本語出て行け!" );
}
if(preg_match($ruattern, $incoming_comment['comment_content'])){
wp_die( "Russians, get away!Savage выйти из Русского Севера!" );
}
if(preg_match($arattern, $incoming_comment['comment_content'])){
wp_die( "Please do not use Arabic!!من فضلك لا تستخدم اللغة العربية" );
}
if(preg_match($thattern, $incoming_comment['comment_content'])){
wp_die( "Please do not use Thai!กรุณาอย่าใช้ภาษาไทย!" );
}
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "Please write some chinese words!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'syz_comment_post');
如以上,可防止日文、俄文、阿拉伯文字等评论。
四、评论回复邮件通知
//WordPress 评论回复邮件通知代码:使用 smtp 发邮件实现评论通知
add_action('phpmailer_init', 'fanly_mail_smtp');
function fanly_mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用 SMTPAuth 服务
$phpmailer->Port = 465;//SMTP 邮件发送端口, SSL 加密端口为465
$phpmailer->SMTPSecure ="ssl";//验证 SSL
$phpmailer->Host = "smtp.qiye.aliyun.com";// SMTP 服务器地址
$phpmailer->Username = "admin@eeee.me";// SMTP 发件邮箱地址
$phpmailer->Password ="******";//三方客户端安全密码
}
add_filter( 'wp_mail_from', 'fanly_wp_mail_from' );
function fanly_wp_mail_from() {
return 'admin@eeee.me';// SMTP 发件邮箱地址
}
//WordPress 评论回复邮件通知代码:回复配置
function fanly_comment_mail_notify($comment_id) {
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = trim(get_comment($parent_id)->comment_author) . ',您在 [' . $blogname . '] 中的留言有新的回复啦!';
$message = '<div style="color:#555;font:12px/1.5 微软雅黑,Tahoma,Helvetica,Arial,sans-serif;max-width:550px;margin:50px auto;border-top: none;" ><table border="0" cellspacing="0" cellpadding="0"><tbody><tr valign="top" height="2"><td valign="top"><div style="background-color:white;border-top:2px solid #00A7EB;box-shadow:0 1px 3px #AAAAAA;12px;max-width:550px;color:#555555;font-family:微软雅黑, Arial;;font-size:12px;">
<h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:8px 0 10px 8px;"><span style="color: #00A7EB;font-weight: bold;">> </span>您在 <a style="text-decoration:none; color:#58B5F5;font-weight:600;" href="' . home_url() . '">' . $blogname . '</a> 的留言有回复啦!</h2><div style="padding:0 12px 0 12px;margin-top:18px">
<p>您好, ' . trim(get_comment($parent_id)->comment_author) . '! 您发表在文章 《' . get_the_title($comment->comment_post_ID) . '》 的评论:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">' . nl2br(strip_tags(get_comment($parent_id)->comment_content)) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回复如下:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">' . nl2br(strip_tags($comment->comment_content)) . '</p>
<p>您可以点击 <a style="text-decoration:none; color:#5692BC" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回复內容</a>,也欢迎再次光临 <a style="text-decoration:none; color:#5692BC"
href="' . home_url() . '">' . $blogname . '</a>。思意博客博客祝您生活愉快!</p>
<p style="padding-bottom: 15px;">(此邮件由系统自动发出,请勿直接回复!)</p></div></div></td></tr></tbody></table></div>';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $message, $headers );
}
}
add_action('comment_post', 'fanly_comment_mail_notify');
通过以上设置,在评论者留了邮箱的情况下,如果对此评论进行回复,评论者邮箱即可收到对该评论回复的通知邮件。经测试,使用 QQ 邮箱、网易邮箱、飞书邮箱等,“邮件原文”会暴露服务器 ip 地址,而免费的阿里云企业邮箱则不会。
五
禁用 xmlrpc.php
// 禁用 xmlrpc.php add_filter( 'xmlrpc_enabled', '__return_false' );