如何在WordPress中轻松添加自定义代码(不破坏您的网站)

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
自定义代码段的问题 (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

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 snippetAdding your first code snippet

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

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

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

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

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

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视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值