隐藏WordPress主题和插件目录

多数情况下,主题或插件的静态文件(css、js、图片之类的),会保存在主题或插件目录下。这样很轻易的就暴露了我们使用的什么插件或主题。所以,我们想个办法把这个路径隐藏起来。下面来看下如何在 Nginx的伪静态规则中实现这一目的。

添加伪静态规则

 
if (!-e $request_filename) {
    rewrite ^/assets/css/(.*)$ /wp-content/themes/theme-name/assets/css/$1 last;
    rewrite ^/assets/js/(.*)$ /wp-content/themes/theme-name/assets/js/$1 last;
    rewrite ^/assets/img/(.*)$ /wp-content/themes/theme-name/assets/img/$1 last;
    rewrite ^/plugins/(.*)$ /wp-content/plugins/$1 last;
    break;
}

在 Nginx伪静态规则中,添加上面的代码即可。注意替换其中的路径为你自己的。

修改主题或插件中调用静态资源的代码

把 get_template_directory_uri() 修改为 home_url(),即可。如下示例:

 
//wp_enqueue_style( 'BootStrap', get_template_directory_uri() . '/assets/css/bootstrap.min.css', array(), '4.2.1' );
wp_enqueue_style( 'BootStrap', home_url() . '/assets/css/bootstrap.min.css', array(), '4.2.1' );

使用钩子一劳永逸

我们也可以使用WordPress提供的hook钩子来对 get_template_directory_uri() 之类的方法获取到的地址进行修改,但是要注意不要影响其他功能。简单的方法就是判断地址中是否包含指定字符。

posted on   剩余价值  阅读(509)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示