本文档介绍了在 WordPress 中编辑文件的方法,包括使用内置编辑器在线编辑和离线编辑文件。重点强调了备份、文件权限、编辑器选择和安全注意事项,以帮助开发者安全有效地修改主题和插件文件。
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:
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.
You can find these editors in the following places, depending on your theme:
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.
The following file types (if writable) can be edited in the plugin editor that is built into the WordPress administrative panel:
In the theme editor, only writable PHP and CSS files can be edited.
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.
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.
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.
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.
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.
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.
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.
The following text editors are acceptable for file editing: