跳至正文

How to Rename Your WordPress WP-Content Directory

How to Rename Your WordPress WP-Content Directory

If you have been working with WordPress, you probably already know that all assets like images, stylesheets, themes, and plugins in WordPress are by default, stored under the wp-content directory. But, do you know that we can actually change this folder name into something else, and yet it will still work?

One of the advantages of renaming “wp-content/” is that your website will look less WordPress-ish, while others say that this could even make your site more secure.

In this post, we will show you how to do it.

Create a New Folder

In this example, we will rename the wp-content folder to assets. Note that after renaming this folder, any activated plugin or theme will be deactivated and will not be listed in their respective pages, since WordPress cannot find them from the default location, wp-content.

Config Modification

We need to make a few modifications to wp-config.php, but, it is better to create a backup of this file before making any change. Just in case something wrong happen, we will be able to restore it with the backup.

首先,在前面添加以下行require_once(ABSPATH . 'wp-settings.php');(通常位于最底部)以告诉 WordPress wp-content已更改为assets

1个
define ('WP_CONTENT_FOLDERNAME', 'assets');

然后,在下面添加这一行以将 WordPress 定向到新的目录路径。

1个
define ('WP_CONTENT_DIR', ABSPATH . WP_CONTENT_FOLDERNAME) ;

添加以上行后,WordPress 将查找并列出已安装的主题和插件。

但是,正如您在主题屏幕截图(见下文)中所见,链接仍然断开。

要解决此问题,请添加此行以指定新目录 URL。

1个
2个
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_CONTENT_URL', WP_SITEURL . WP_CONTENT_FOLDERNAME);

我们完了。现在,我们上传的所有插件、主题和任何媒体都将存储在我们的新文件夹中。

必须注意!

There are several plugins and themes that do not follow best practices. They specify “wp-content” as the path and url in their code, instead of defining them dynamically. In such cases, the plugins and themes may not function properly. Furthermore, this will also break links to images that already attached in your posts and that are stored in wp-content.

So, this modification should be taken with caution and, it is better done when you just starting your website. Otherwise, it could ruin your website entirely, and there will be tons of things you have to fix.

标签: