WordPress 关闭用户注册和修改密码向管理员发邮件

打开 wp-incloudes/pluggable.php 文件

注册发送邮件

$wp_new_user_notification_email_admin = array(
	'to'      => get_option( 'admin_email' ),
	/* translators: Password change notification email subject. %s: Site title 
	'subject' => __( '[%s] New User Registration' ),
	'message' => $message,
	'headers' => '',
);

修改密码发送邮件

$wp_password_change_notification_email = array(
	'to'      => get_option( 'admin_email' ),
	/* translators: Password change notification email subject. %s: Site title 
	'subject' => __( '[%s] Password Changed' ),
	'message' => $message,
	'headers' => '',
);

将上面两段代码使用 /* .... */ 注释掉即可。WordPress 更新的时候可能会覆盖,需要再次修改。

WordPress 限制搜索关键词

今天打开 Google Search Console 的后台查看网页索引编制,给吓了一跳。这群祸害硬生生的给关键词刷了两万多条,怎么不去死啊。

zapro_231223151921

zapro_231223151629

所以决定对搜索加以修改,限制搜索内容的字符长度和过滤部分关键词。将以下代码添加到你主题下的functions.php文件中:

//WordPress限制搜索关键词实现搜索黑名单
function dmd_search_filter($request_vars) {
    if (!is_user_logged_in()){
        $request_vars['s'] = "请先登录";
        }
    if (!empty($request_vars['s']) && iconv_strlen($request_vars['s'],"UTF-8")>5) {
        $request_vars['s'] = "搜索词太长";
    }
    $a=array("色情","赌博","政治");
    for($i=0;$i

三行代码提高 WordPress 的后台安全性

由于 WordPress 本身的易用性,用户能够直接在 WordPress 后台快速编辑主题和插件文件,同时还可以方便地在后台进行主题和插件的安装、更新和删除。然而,这也意味着后台可能存在潜在的安全风险。以下的三行代码对提升 WordPress 后台的安全性非常有帮助。

//WordPress 后台安全性提升
define('FORCE_SSL_ADMIN', true); //强制后台使用 SSL 连接
define('DISALLOW_FILE_EDIT', true); //禁用后台主题和插件编辑器
define('DISALLOW_FILE_MODS', true); //禁止后台安装、更新、删除插件和主题

将以上代码插入到WordPress的根目录配置文件wp-config.php中,具体的位置就是
“/* That’s all, stop editing! Happy publishing. */”

“/* 好了!请不要再继续编辑。请保存本文件。使用愉快! */”
这行代码上方即可。

需要注意的是,FORCE_SSL_ADMIN这一行是用于启用后台使用HTTPS访问的。如果在本地或非正式环境中没有安装SSL证书,请勿使用这一行,否则将导致后台无法访问。

修改 WordPress 内存限制提升网站性能

对于 WordPress 拥有上万数据量的时候,一次性调用和处理大数据的时候就会出现超时和内存溢出、502 错误等,那么为了充分利用服务器资源和更好的发挥 WordPress 性能,我可以通过修改 WordPress 内存限制来提升 WordPress 性能。

修改方法

在 WordPress 根目录的 wp-config.php 文件有“ABSPATH”字样的上面添加一下配置代码即可:

//WordPress 内存限制
define( 'WP_MEMORY_LIMIT', '128M' ); //前端
define( 'WP_MAX_MEMORY_LIMIT', '256M' ); //后端

使用WP CLI命令,一次性删除 WordPress 中未使用的图片

删除多余的图片,实际上数据库也进行了精简,可以让WordPress网站加载更快,也是一种优化方法。

for id in $(wp db query "SELECT ID FROM wp_posts WHERE post_date>='2022-10-01' AND post_date<='2022-10-30' AND post_type='attachment' AND post_parent=0" --silent --skip-column-names)
do
wp post delete --force $id
done

删除 WordPress 插入的图像宽度和高度属性

通过 WordPress 媒体上传器上传图像,然后将其插入编辑器时,它带有宽度和高度属性。这些通常是可取的,因为它有助于浏览器在布局期间为图像腾出适当的空间。但是,如果您想从添加这些属性中删除插入操作,您可以将此代码添加到您的functions.php文件或您自己制作的功能插件中:

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
 
function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;
}

WordPress 普通用户登录后转到指定页面

除管理员外,其它帐号登录后转到一个指定页面。

将下面代码放入主题 functions.php

function theme_login_redirect( $url, $request, $user ){
    if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
        if( $user->has_cap( 'administrator' ) ) {
            $url = admin_url();
        } else {
            $url = home_url('/custom-page/');
        }
    }
    return $url;
}
add_filter('login_redirect', 'theme_login_redirect', 10, 3 );

如果希望对普通用户隐藏顶部工具栏,继续添加下面代码。

if ( ! current_user_can( 'manage_options' ) ) {
    show_admin_bar( false );
}

WordPress 文章内URL自动超链接自动添加nofollow在新窗口打开

WordPress文章默认需要给链接插入连接才可以生成超链接,非常繁琐,现在只需几行代码,就有可以自动给文章中的所有连接添加a标签,并且加上nofollow和_blank属性,让连接自动在新窗口打开并且不传递权重。

修改主题的functions.php文件,在最后加入:

// 文章页面链接添加nofollow标签并在新窗口打开
add_filter('the_content', 'make_clickable');
function autolinkraaynk( $content ) {
    $regexp = "]*href=(\"??)([^\" >]*?)\\1[^>]*>";
    if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
        if( !empty($matches) ) {
            $srcUrl = get_option('siteurl');
            for ($i=0; $i < count($matches); $i++)
            {
                $tag = $matches[$i][0];
                $tag2 = $matches[$i][0];
                $url = $matches[$i][0];
                $noFollow = '';
                $pattern = '/target\s*=\s*"\s*_blank\s*"/';
                preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
                if( count($match) < 1 )
                    $noFollow .= ' target="_blank" ';
                $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
                preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
                if( count($match) < 1 ) $noFollow .= ' rel="nofollow" '; $pos = strpos($url,$srcUrl); if ($pos === false) { $tag = rtrim ($tag,'>');
                    $tag .= $noFollow.'>';
                    $content = str_replace($tag2,$tag,$content);
                }
            }
        }
    }
    $content = str_replace(']]>', ']]>', $content);
    return $content;
}
add_filter( 'the_content', 'autolinkraaynk');

WordPress 常用优化代码

优化代码添加到主题目录functions.php文件

/*彻底关闭自动更新(核心程序/主题/插件/翻译自动更新*/
add_filter('automatic_updater_disabled', '__return_true');
/*关闭更新检查定时作业*/
remove_action('init', 'wp_schedule_update_checks');
/*移除已有的版本检查定时作业*/
wp_clear_scheduled_hook('wp_version_check');
/*移除已有的插件更新定时作业*/
wp_clear_scheduled_hook('wp_update_plugins');
/*移除已有的主题更新定时作业*/
wp_clear_scheduled_hook('wp_update_themes');
/*移除已有的自动更新定时作业*/
wp_clear_scheduled_hook('wp_maybe_auto_update');
/*移除后台内核更新检查*/
remove_action( 'admin_init', '_maybe_update_core' );
/*移除后台插件更新检查*/
remove_action( 'load-plugins.php', 'wp_update_plugins' );
remove_action( 'load-update.php', 'wp_update_plugins' );
remove_action( 'load-update-core.php', 'wp_update_plugins' );
remove_action( 'admin_init', '_maybe_update_plugins' );
/*移除后台主题更新检查*/
remove_action( 'load-themes.php', 'wp_update_themes' );
remove_action( 'load-update.php', 'wp_update_themes' );
remove_action( 'load-update-core.php', 'wp_update_themes' );
remove_action( 'admin_init', '_maybe_update_themes' );
/*关闭程序更新提示*/
add_filter( 'pre_site_transient_update_core', function($a){ return null; });
/*关闭插件更新提示*/
add_filter('pre_site_transient_update_plugins', function($a){return null;});
/*关闭主题更新提示*/
add_filter('pre_site_transient_update_themes', function($a){return null;});
//关闭WordPress的XML-RPC功能
add_filter('xmlrpc_enabled', '__return_false');
//关闭XML-RPC的 pingback端口
add_filter( 'xmlrpc_methods', 'remove_xmlrpc_pingback_ping' );
function remove_xmlrpc_pingback_ping( $methods ) {
unset( $methods['pingback.ping'] );
return $methods;
}
//移除前端网页源代码内的头部冗余代码
remove_action( 'wp_head', 'feed_links_extra', 3 ); 
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 ); 
remove_action( 'wp_head', 'wp_generator' ); 
//移除新版本站点健康状态面板和菜单项
add_action( 'admin_menu', 'remove_site_health_menu' ); 
function remove_site_health_menu(){
remove_submenu_page( 'tools.php','site-health.php' ); 
}
//禁用5.5版后自带的XML站点地图
add_filter( 'wp_sitemaps_enabled', '__return_false' );
//移除后台仪表盘站点健康状态面板
add_action('wp_dashboard_setup', 'remove_site_health_dashboard_widget');
function remove_site_health_dashboard_widget()
{
    remove_meta_box('dashboard_site_health', 'dashboard', 'normal');
}
//移除后台仪表盘菜单:站点健康状态
add_action( 'admin_menu', 'remove_site_health_menu' );	
function remove_site_health_menu(){
	remove_submenu_page( 'tools.php','site-health.php' ); 
}
//移除后台仪表盘菜单:活动、新闻
function bzg_remove_dashboard_widgets() {
	global $wp_meta_boxes;
	#移除 "活动" 
	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
	#移除 "WordPress 新闻" 
	unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
}
add_action('wp_dashboard_setup', 'bzg_remove_dashboard_widgets' );
//移除后台仪表盘菜单:帮助
function bzg_remove_help() {
	get_current_screen()->remove_help_tabs();
}
add_action('admin_head', 'bzg_remove_help');

//移除后台页面title标题的wordpress后缀
add_filter('admin_title', 'delAdminTitle', 10, 2);
function delAdminTitle($admin_title, $title){
    return $title.' ‹ '.get_bloginfo('name');
}
//移除登陆页面title标题的wordpress后缀
add_filter('login_title', 'remove_login_title', 10, 2);
function remove_login_title($login_title, $title){
	return $title.' ‹ '.get_bloginfo('name');
}
//切换经典文章编辑器(v5.x开始默认古腾堡编辑器)
add_filter('use_block_editor_for_post', '__return_false');
//禁止WordPress新版本文章编辑器前端加载样式文件
remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
//替换评论用户头像链接为国内镜像加速访问
add_filter('get_avatar', function ($avatar) {
return str_replace([
'www.gravatar.com/avatar/',
'0.gravatar.com/avatar/',
'1.gravatar.com/avatar/',
'2.gravatar.com/avatar/',
'secure.gravatar.com/avatar/',
'cn.gravatar.com/avatar/'
], 'gravatar.wp-china-yes.net/', $avatar);
});

WordPress 5.8 切换小工具Widgets经典模式

WordPress官方特意推出了Classic Widgets这个插件,只需要安装并且激活就行了。

插件下载:https://wordpress.org/plugins/classic-widgets/

如果不想用插件,直接复制下面代码添加到主题函数文件里面保存也可以。

// Disables the block editor from managing widgets in the Gutenberg plugin.
add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' );
// Disables the block editor from managing widgets.
add_filter( 'use_widgets_block_editor', '__return_false' );
退出移动版