Often while reading WordPress tutorials, you may be asked to add custom code snippets in your theme’s functions.php file or in a site-specific plugin. The problem is that even a slightest mistake can break your website. In this article, we will show you an easy way to add custom code in WordPress without breaking your site.
通常在阅读WordPress教程时 ,可能会要求您在主题的functions.php文件或特定于站点的插件中添加自定义代码段。 问题在于,即使是最轻微的错误也会破坏您的网站。 在本文中,我们将向您展示一种在WordPress中添加自定义代码的简单方法,而不会破坏您的网站。
![How to easily add custom code in WordPress How to easily add custom code in WordPress](https://i-blog.csdnimg.cn/blog_migrate/7a2a8009b90511145636232e55393886.png)
自定义代码段的问题 (The problem with Custom Code Snippets)
Often you’ll find code snippets in WordPress tutorials with instructions to add them into your theme’s functions.php file or a site-specific plugin.
通常,您会在WordPress教程中找到代码片段,并附有将其添加到主题的functions.php文件或特定于站点的插件中的说明 。
The problem is that even a tiny mistake in the custom code snippet can break your WordPress site and make it inaccessible.
问题在于,即使是自定义代码段中的一个小错误也可能破坏您的WordPress网站并使其无法访问。
The other problem is that if you add multiple code snippets in a site-specific plugin, then it can become hard to manage the file.
另一个问题是,如果您在特定于站点的插件中添加多个代码片段,则可能难以管理文件。
Recently while looking for a solution, we discovered an easy for users to add and manage custom code snippets in WordPress.
最近,在寻找解决方案时,我们发现用户可以轻松地在WordPress中添加和管理自定义代码段。
在WordPress中添加自定义代码段 (Adding Custom Code Snippets in WordPress)
First thing you need to do is install and activate the Code Snippets plugin on your website. For more details, see our step by step guide on how to install a WordPress plugin.
您需要做的第一件事是在您的网站上安装并激活Code Snippets插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。
Upon activation, the plugin will add a new menu item labeled Snippets into your WordPress admin bar. Clicking on it will show you a list of all the custom code snippets you have saved on your site.
激活后,该插件将在您的WordPress管理栏中添加一个名为“代码段”的新菜单项。 单击它会为您显示保存在站点上的所有自定义代码段的列表。
Since you just installed the plugin, your list will be empty.
由于您刚刚安装了插件,因此您的列表将为空。
Go ahead and click on the Add New button to add your first custom code snippet in WordPress.
继续并单击“添加新”按钮以在WordPress中添加您的第一个自定义代码段。
![Add new custom code snippet Add new custom code snippet](https://i-blog.csdnimg.cn/blog_migrate/7af557e87a5393f1b082a0437a73f5f2.png)
This will bring you to the ‘Add New Snippet’ page.
这将带您进入“添加新代码段”页面。
You need to start by entering a title for your custom code snippet. This could be anything that helps you identify the code. After that you can copy and paste your code snippet into the code box.
您需要先输入自定义代码段的标题。 这可以帮助您识别代码。 之后,您可以将代码段复制并粘贴到代码框中。
![Adding your first code snippet Adding your first code snippetAdding your first code snippet](https://i-blog.csdnimg.cn/blog_migrate/613c1b2f76dc056cc85a4fbd48d58405.png)
In the screenshot above, we have added a custom code snippet to remove the WordPress version number from our test site.
在上面的屏幕截图中,我们添加了一个自定义代码段,以从我们的测试站点中删除WordPress版本号 。
function wpb_remove_version() {
return '';
}
add_filter('the_generator', 'wpb_remove_version');
Below the code box, you will see a text area to add description. You can add anything here that helps you understand what this code does, where you found it, and why you are adding it to your website.
在代码框下方,您将看到一个文本区域以添加描述。 您可以在此处添加任何内容,以帮助您理解该代码的功能,发现的位置以及将其添加到网站的原因。
![Add code description Add code description](https://i-blog.csdnimg.cn/blog_migrate/6f4316f3439858b78fefdc11da852e5a.png)
You can also assign tags to your code snippet. This will help you sort your code snippets by topic and functionality.
您还可以为代码段分配标签 。 这将帮助您按主题和功能对代码段进行排序。
Finally, you can click on the ‘Save Changes and Activate’ button.
最后,您可以点击“保存更改并激活”按钮。
If you just want to save the code snippet and not activate it, then you can click on the ‘Save Changes’ button.
如果您只想保存代码片段而不激活它,则可以单击“保存更改”按钮。
You can also change the scope of the code snippet. You can run it only in the WordPress admin area, on the front-end of your site, or everywhere. If you are unsure, then keep the default ‘Run snippet everywhere’ option checked.
您还可以更改代码段的范围。 您只能在WordPress 管理区域 ,网站前端或任何地方运行它。 如果不确定,请选中默认的“到处运行代码段”选项。
Once you have saved and activated the code snippet, it would become effective on your site immediately.
保存并激活代码段后,该代码段将立即在您的站点上生效。
处理自定义代码中的错误 (Handling Errors in Custom Code)
Normally, if you make a mistake in adding the custom code in your site-specific plugin or theme file, it would immediately make your site inaccessible.
通常,如果在将自定义代码添加到特定于站点的插件或主题文件中时出错,则会立即使您的站点无法访问。
You’d start seeing a syntax error or a 500 internal server error on your site. To fix this you’ll need to manually undo your code using an FTP client.
您会开始在网站上看到语法错误或500个内部服务器错误 。 要解决此问题,您需要使用FTP客户端手动撤消代码。
The neat part about the Code Snippets plugin is that it will automatically detect a syntax error in the code and immediately deactivate it.
关于代码片段插件的简洁之处在于它将自动检测代码中的语法错误并立即将其停用。
![Error handling in your custom code snippet Error handling in your custom code snippet](https://i-blog.csdnimg.cn/blog_migrate/6e602da0c52f3929ed0916a996abcfea.png)
It will also show you a helpful error message, so you can debug the error.
它还将向您显示一条有用的错误消息,以便您可以调试错误。
管理您的自定义代码段 (Managing Your Custom Code Snippets)
Code Snippets plugin provides an easier graphical user interface to manage your custom code snippets in WordPress.
代码片段插件提供了更简单的图形用户界面,可在WordPress中管理您的自定义代码片段。
You can save code snippets without activating them on your site. You can deactivate the snippet at any time you want. You can also export specific code snippets or bulk export all of them.
您可以保存代码段,而无需在您的站点上激活它们。 您可以随时停用该代码段。 您还可以导出特定的代码段或批量导出所有代码段。
![Export your custom code snippets Export your custom code snippets](https://i-blog.csdnimg.cn/blog_migrate/d70818f6b2f680ccd799101947dc249b.png)
If you’re moving websites to a different server, you can easily import your code snippets using the Code Snippets plugin by visiting Snippets » Import page.
如果要将网站移动到其他服务器 ,则可以通过访问“ 代码段»导入”页面,使用“代码段”插件轻松导入代码段。
![Import Snippets Import Snippets](https://i-blog.csdnimg.cn/blog_migrate/92521dfae6363b9dac00c9f7357791a5.png)
Code Snippets plugin work out of the box, but you can tweak the plugin settings by visiting the Snippets » Settings page.
Code Snippets插件可以直接使用,但是您可以通过访问Snippets»设置页面来调整插件设置。
![Tweaking plugin settings Tweaking plugin settings](https://i-blog.csdnimg.cn/blog_migrate/4d1a9c3ee63904942150529e1eb61d42.png)
We hope this article helped you learn how to easily add custom code in WordPress. Want to experiment with some code snippets on your website? Check out our list of extremely useful tricks for the WordPress functions file.
我们希望本文能帮助您学习如何轻松地在WordPress中添加自定义代码。 是否想在您的网站上尝试一些代码片段? 查看我们的WordPress函数文件非常有用的技巧列表。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。