高级管理文档

💡 云策文档标注

概述

本文档介绍了在 WordPress 中编辑文件的方法,包括使用内置编辑器在线编辑和离线编辑文件。重点强调了备份、文件权限、编辑器选择和安全注意事项,以帮助开发者安全有效地修改主题和插件文件。

关键要点

  • WordPress 提供内置的 Theme File Editor 和 Plugin File Editor,允许通过浏览器在线编辑文件,但需注意用户权限和文件可写性。
  • 编辑前务必备份文件,建议使用子主题(Child Theme)来保护修改不被主题更新覆盖。
  • 仅可编辑特定文件类型:在插件编辑器中可编辑 HTML、PHP、CSS、TXT 等;在主题编辑器中仅可编辑 PHP 和 CSS 文件。
  • 内置编辑器功能基础,无高级特性如语法高亮,建议使用浏览器搜索功能辅助查找代码。
  • 文件权限需设置为可写(至少 604)才能使用内置编辑器编辑,可通过 FTP、SSH 或主机文件管理器修改。
  • 编辑错误时,应使用备份文件恢复或从原始来源下载新副本,并始终优先备份。
  • 为安全起见,可通过在 wp-config.php 中添加 DISALLOW_FILE_EDIT 函数禁用内置编辑器。
  • 离线编辑推荐使用文本编辑器(如 Notepad++、Visual Studio Code),避免使用字处理软件(如 Microsoft Word),以防文件损坏。
  • 不建议修改 WordPress 核心文件(除 wp-config.php 外),如需修改应记录更改并备份核心文件。

注意事项

  • 编辑文件时,更改会立即生效,建议离线测试后再上传以确保安全。
  • 使用内置编辑器时,注意其功能限制,并利用浏览器搜索功能提高效率。
  • 始终在编辑前备份文件,以防意外错误导致网站崩溃或无法访问。

📄 原文内容

There are times when you will need to edit WordPress files, especially if you want to change your WordPress Theme. WordPress features a built-in editor that allows you to edit files online, using any internet browser. You can also edit files copied or stored on your computer, and then upload them to your site using an FTP client.

Before editing any of your WordPress files, be sure to do the following:

  • Work from copies of backup files when possible, and make sure that you back up your information frequently–while you work, and whenever you make changes. Remember to keep your backups in a safe place!
  • When working online, you need to set the appropriate file permissions so that you can modify and save files. If you see a note at the bottom of the WordPress editor panel that says If this file was writable, you could edit it… this means that you need to change the file permissions before you can make any changes.
  • When modifying files outside the built-in plugin and theme editors, use a text editor. It is strongly advisable not to use a word-processing program. Word processors change quote marks to characters, they sometimes convert specific characters, and they can also add in unwanted code. These changes can cause files to break. For similar reasons, it is also inadvisable to use certain HTML generator programs.

Using the Theme File Editor and Plugin File Editor

WordPress contains two built-in editors that allow you to edit theme files directly from your browser. They are called the theme file editor and the plugin file editor.

Please note that, depending on the level of user privileges that you have, you may or may not be able to access these features in the administrative panel of your blog. Please contact your blog or website administrator, to have your privileges adjusted.

Where can I find these editors?

You can find these editors in the following places, depending on your theme:

  • If you are using a Block theme, both the Theme and Plugin File Editor will be listed under Tools.
  • If you are using a Classic theme, the Theme File Editor will be listed under Appearance and the Plugin File Editor will be listed under Plugins.

You can view a file in either of these editors by accessing it from the right-hand sidebar navigation.

More information on editing themes is available at Theme Developer Handbook.

Be aware that if the theme you edit is updated, your changes will be overwritten. To better organize your changes and protect them from updates, consider creating a Child Theme in which to keep all your changes.

What Files Can Be Edited?

The following file types (if writable) can be edited in the plugin editor that is built into the WordPress administrative panel:

  • HTML
  • PHP
  • CSS
  • TXT (and related text-like files such as RTF)

In the theme editor, only writable PHP and CSS files can be edited.

Things You Need to Know

Instant Changes

The changes you make to files using the WordPress editors are instant. The changes happen online, in real-time. You and any visitors to your site will see the changes, immediately.

Because of the immediate nature of the changes, it’s usually safer to edit copies of your files offline, test the file copies, and then upload your changes when they are verified.

Always make sure you have a current backup before editing files.

Editor Features

The built-in WordPress plugin and theme file editors are very basic, allowing you to easily view and edit plugin and theme files on your website. Please note that there are no advanced editor features such as search and replace, line numbers, syntax highlighting, or code completion.

Hint: Use your browser’s internal search bar to help find code within the visual editors.

File Permissions

To edit a file using the built-in WordPress plugin and theme editors, the permissions for that file must be set to writable (at least 604). You can change the permissions on files by using an FTP client program, a web-based file manager provided by your host, or from the command-line using SSH (secure shell). Your options depend on the type of access your host offers.

Make a Mistake? Use Backup Files

Back up all files before editing. If you make a mistake that causes errors, causes a site crash, creates a blank screen, or blocks access to your WordPress Dashboard, delete the changed file and replace it with a good copy from your backup.

No backup? Download a fresh copy of the file you edited from the original source, replace it, and start over. BACK UP FIRST.

Security Warning

By default, any user that logs in with administrative permissions can access the WordPress plugin and theme editors, and change any theme or plugin file on your site in real-time.

To combat accidents, errors, or even hacking, you may wish to disable the ability to edit files within the WordPress theme by adding the DISALLOW_FILE_EDIT function to your wp-config.php file.

Editing Files Offline

To edit files offline, you can use any of the recommended text editors to create and edit files, and an FTP client to upload them. Make sure to view the results in your browser, to see if the desired changes have taken effect.

Note: It is not recommended to change WordPress core files other than wp-config.php. If you must change anything else, take notes about your changes, and store a copy of these notes in a text file in your WordPress root directory. You should also make a backup copy of your WordPress core files, for future reference and upgrades.

Using Text Editors

Editors to Avoid

Editors to avoid include any do-it-yourself instant web page software (like Adobe Dreamweaver), or text processor (like Google Docs or Microsoft Word).

Note: If you use an external editor such as a word-processor to create and edit files, this can corrupt the file you are editing. See text editor in the glossary for a short explanation as to why you should avoid these editors.

Text Editors

The following text editors are acceptable for file editing: