如何将自定义JavaScript添加到您的WordPress网站?

When you want to change the theme of your site, one thing that needs to be done is to add custom JavaScript to it. However, there are other options available if you do not want to add it to your site. For instance, simply add the ‘Heat and Footer Script’ as an option which is much less intrusive compared to the other.

当您想要更改网站的主题时,需要做的一件事是向其添加自定义JavaScript。 但是,如果您不想将其添加到站点,则还有其他选项可用。 例如,只需添加“ Heat and Footer Script”作为选项,与其他选项相比,其侵入性要小得多。

Well, this article is not about the various alternatives, but mainly about the JavaScript which can be added to your WordPress site. How you can exactly do the task? We will share the details with you in the section below.

好吧,本文不是关于各种替代方案,而是主要关于可以添加到WordPress网站JavaScript。 您如何才能准确地完成任务? 我们将在下面的部分中与您分享详细信息。

将JavaScript添加到WordPress网站的选项 (Options to add JavaScript to WordPress Site)

如何将自定义JavaScript添加到您的WordPress网站?

Image Source

图片来源

It is not difficult to add custom JavaScript to a WordPress site. All you need to know the way through which it can be done. Two main options and ways in which you can add JavaScript to WordPress sites include the following:

将自定义JavaScript添加到WordPress网站并不难。 您需要知道的所有方法。 可以将JavaScript添加到WordPress网站的两个主要选项和方法如下:

1.添加自定义JavaScript文件 (1. Add Custom JavaScript File)

First of all, you have to save the file with .js format. After this, you may add this into the folder http://wp-content/themes/your-theme/js/. When you do this, go to the settings where there will be two options in front of you. Either you can add it to the header, or the footer. This is how simple it can be.

首先,您必须以.js格式保存文件。 之后,您可以将其添加到文件夹http:// wp-content / themes / your-theme / js /中 。 执行此操作时,请转到设置,其中前面有两个选项。 您可以将其添加到页眉或页脚。 这是多么简单。

2.添加不带文件的自定义JavaScript (2. Add Custom JavaScript Without File)

This option is simple to add the custom JavaScript without file. You do not need to save this file as .js format. Rather, you can directly upload it to the folder http://wp-content/themes/your-theme/js/. The header will be the place where you will add it and then go to the save setting when you complete the process.

此选项很容易添加不带文件的自定义JavaScript。 您无需将此文件另存为.js格式。 相反,您可以将其直接上传到文件夹http:// wp-content / themes / your-theme / js / 。 标头将是您添加标头的位置,然后在完成该过程后转到保存设置。

你不应该做的一件事 (One Thing You Should Never Do)

Although it is quite clear that a much better option to add the custom JavaScript to your WordPress site is to use the ‘SCRIPT’ tag. This is added directly into either header or footer.php. However, you should never do that. As WordPress has a specific loading sequence, therefore, this sequence needs to be respected, and this option must not be availed.

虽然很明显,将自定义JavaScript添加到WordPress网站的更好选择是使用'SCRIPT'标签。 将其直接添加到header或footer.php中。 但是,您绝对不要这样做。 由于WordPress具有特定的加载顺序,因此,必须遵守此顺序,并且不得使用此选项。

The reason behind this is that if you will add to the header or footer.php which will lead to changes within the whole theme. The core and plugins of WordPress might also face conflicts due to the addition of custom JavaScript in this way. Instead of this option that brings so many issues in your WordPress site, therefore, try to use only the above-mentioned two options. Make sure that you do not add it to the header or footer, even if it seems the only option.

这背后的原因是,如果您将添加到header或footer.php中,这将导致整个主题内的更改。 由于以这种方式添加了自定义JavaScript,WordPress的核心和插件也可能会面临冲突。 因此,请尝试仅使用上述两个选项,而不是在WordPress网站中带来很多问题的此选项。 确保不要将其添加到页眉或页脚,即使它似乎是唯一的选择。

When it comes to importing templates, getting a reliable WordPress website builder will allow you to import templates for just about any topic or any niche with endless customization options.

当涉及到导入模板时,获得可靠的WordPress网站构建器将使您可以使用无穷的自定义选项为几乎任何主题或任何细分市场导入模板。

结论 (Conclusion)

As you can see that the custom JavaScript is not difficult to add to the WordPress site. It will take a few minutes if you know how to do it. We have explained the information in detail where you will get to know about each point. Follow the step by step, and do not make amendments on your own.

如您所见,自定义JavaScript添加到WordPress网站并不困难。 如果您知道该怎么做,将需要几分钟。 我们已经详细解释了信息,您将在其中了解每个点。 请循序渐进,不要自己修改。

翻译自: https://www.thecrazyprogrammer.com/2020/06/how-to-add-custom-javascript-into-your-wordpress-site.html