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证书,请勿使用这一行,否则将导致后台无法访问。

解决Debian 11 apt-get 更新 404 错误

root@Bser8608377298916:~# apt-get update
Ign:1 http://security.debian.org bullseye/updates InRelease
Err:2 http://security.debian.org bullseye/updates Release
  404  Not Found [IP: 151.101.26.132 80]
Get:3 http://deb.debian.org/debian bullseye InRelease [116 kB]
Get:4 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:5 http://deb.debian.org/debian bullseye-backports InRelease [49.0 kB]
Get:6 http://deb.debian.org/debian bullseye/main Sources [8634 kB]
Get:7 http://deb.debian.org/debian bullseye/main amd64 Packages [8183 kB]
Get:8 http://deb.debian.org/debian bullseye/main Translation-en [6240 kB]
Get:9 http://deb.debian.org/debian bullseye-updates/main Sources [4812 B]
Get:10 http://deb.debian.org/debian bullseye-updates/main amd64 Packages [14.6 kB]
Get:11 http://deb.debian.org/debian bullseye-updates/main Translation-en [7929 B]
Get:12 http://deb.debian.org/debian bullseye-backports/main Sources [420 kB]
Get:13 http://deb.debian.org/debian bullseye-backports/main amd64 Packages [416 kB]
Get:14 http://deb.debian.org/debian bullseye-backports/main Translation-en [348 kB]
Reading package lists... Done
E: The repository 'http://security.debian.org bullseye/updates Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

先备份一下原版的sources.list文件

mv /etc/apt/sources.list /etc/apt/sources.list.old

然后替换一下官方源

cat > /etc/apt/sources.list << EOF
deb http://deb.debian.org/debian/ bullseye main contrib non-free
deb-src http://deb.debian.org/debian/ bullseye main contrib non-free
deb http://deb.debian.org/debian/ bullseye-updates main contrib non-free
deb-src http://deb.debian.org/debian/ bullseye-updates main contrib non-free
deb http://deb.debian.org/debian/ bullseye-backports main contrib non-free
deb-src http://deb.debian.org/debian/ bullseye-backports main contrib non-free
deb http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
deb-src http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
EOF

国内可换成阿里的源

cat > /etc/apt/sources.list << EOF
deb http://mirrors.cloud.aliyuncs.com/debian/ bullseye main contrib non-free
deb-src http://mirrors.cloud.aliyuncs.com/debian/ bullseye main contrib non-free
deb http://mirrors.cloud.aliyuncs.com/debian/ bullseye-updates main contrib non-free
deb-src http://mirrors.cloud.aliyuncs.com/debian/ bullseye-updates main contrib non-free
deb http://mirrors.cloud.aliyuncs.com/debian/ bullseye-backports main contrib non-free
deb-src http://mirrors.cloud.aliyuncs.com/debian/ bullseye-backports main contrib non-free
deb http://mirrors.cloud.aliyuncs.com/debian/ bullseye-proposed-updates main contrib non-free
deb-src http://mirrors.cloud.aliyuncs.com/debian/ bullseye-proposed-updates main contrib non-free
deb http://mirrors.cloud.aliyuncs.com/debian-security/ bullseye-security main contrib non-free
deb-src http://mirrors.cloud.aliyuncs.com/debian-security/ bullseye-security main contrib non-free
EOF

最后apt-get update 一下,完成修复

宝塔面板当前未安装 docker或docker-compose 解决方法

宝塔面板当前未安装 docker或docker-compose 解决方法

宝塔升级到7.9.7以上后,点击安装docker或docker-compose无法正常使用。解决办法如下:

安装之后运行下面的代码,重启面板。

curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-'uname -s'-'uname -m' > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

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

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

修改方法

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

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

宝塔Linux面板关闭软件推荐和广告方法

删除首页软件推荐

文件位置:/www/server/panel/BTPanel/static/js/index.js 使用宝塔编辑器搜索

搜索 s

etTimeout(function () { _this.get_index_list() }, 400) 

取消这部分注释。
搜索 product_recommend.init 函数,注释如下内容。

/*product_recommend.init(function(){
  index.get_product_status(function(){
    index.recommend_paid_version()
  });
  index.get_index_list();
})*/

软件商店广告

文件路径:/www/server/panel/BTPanel/static/js/soft.js 使用宝塔编辑器搜索

if (bt.get_cookie('productPurchase') != null) return false

改为

if (bt.get_cookie('productPurchase') == null) return false

使用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');
退出移动版