WordPress插件vs Functions.php文件(哪个更好?)

We are often asked by users whether they should install a WordPress plugin or add code to their theme’s functions.php file? Some users believe that adding code is always the better method for performance, but that’s actually not true. In this article, we will explain the pros and cons of WordPress plugin vs functions.php file and which method is better.

用户经常问我们,他们应该安装WordPress插件还是将代码添加到主题的functions.php文件中? 一些用户认为添加代码始终是提高性能的更好方法,但实际上并非如此。 在本文中,我们将说明WordPress插件与functions.php文件的优缺点,以及哪种方法更好。

WordPress plugin vs functions file
在Functions.php文件中安装WordPress插件与自定义代码 (Installing WordPress Plugins vs Custom Code in Functions.php File)

You will often find two solutions to add something to your WordPress site. You can either install a WordPress plugin or add a code snippet to your theme’s functions.php file.

您通常会找到两种解决方案,可以向WordPress网站添加内容。 您可以安装WordPress插件,也可以在主题的functions.php文件中添加代码段。

Both methods will do the same thing, and they are both correct. However, many users want to know which one is better for WordPress speed and performance.

两种方法都会做同样的事情,而且都是正确的。 但是,许多用户想知道哪个对WordPress的速度和性能更好。

Some users feel concerned about installing too many plugins, and how it may affect their website speed and WordPress security. Others worry that adding custom code can break their website, and they may not be able to easily fix it.

一些用户担心安装太多的插件 ,以及插件如何影响他们的网站速度和WordPress安全性 。 其他人担心添加自定义代码可能会破坏他们的网站,并且他们可能无法轻松修复它。

Let’s compare both solutions to figure out which one is better for WordPress performance.

让我们比较这两种解决方案,以确定哪种方法更适合WordPress性能。

functions.php文件的优缺点 (Pros and Cons of functions.php File)

WordPress functions.php file

Functions file in WordPress allows theme developers to define custom functionality for their theme. This file acts like a giant WordPress plugin and can be used to add any other custom code snippets you may want to add to your website.

WordPress中的Functions文件允许主题开发人员为其主题定义自定义功能。 该文件的作用就像一个巨大的WordPress插件,可用于添加您可能想添加到您的网站的任何其他自定义代码段。

Here are the advantages of adding custom code to your theme’s functions.php file.

这是将自定义代码添加到主题的functions.php文件中的优点。

Pros

优点

  • You can easily use built-in theme editor in admin area to edit functions.php file

    您可以在管理区域中轻松使用内置主题编辑器来编辑functions.php文件
  • You can copy and paste all code snippets in one file

    您可以将所有代码段复制并粘贴到一个文件中
  • You get a chance to study the code and learn how it works

    您有机会学习代码并学习其工作原理

However, there are also some disadvantages of using functions.php file to save all your custom functionality.

但是,使用functions.php文件保存所有自定义功能也有一些缺点。

Cons

缺点

  • Your custom code will not work if you switch themes

    如果切换主题,您的自定义代码将不起作用
  • Unless you are using a child theme, updating your theme will overwrite functions.php file

    除非您使用子主题,否则更新主题将覆盖functions.php文件
  • It becomes harder to understand where theme code ends and where your custom code begins

    很难理解主题代码在哪里结束以及自定义代码在哪里开始
  • Some code snippets can be too large and need additional scripts and styles

    一些代码片段可能太大,需要其他脚本和样式

Understanding Limitations of WordPress Functions File

了解WordPress函数文件的局限性

There are many handy tricks for the WordPress functions file which make it very tempting. However, it is not a replacement for proper WordPress plugins.

WordPress函数文件有很多方便的技巧,使它非常诱人。 但是,它不能代替适当的WordPress插件。

Adding code snippets to your functions.php file will have the exact same impact on performance, that a WordPress plugin with the same code would have otherwise.

将代码片段添加到您的functions.php文件中将对性能产生完全相同的影响,否则具有相同代码的WordPress插件将具有相同的影响。

Not all functionality and features you need can be added to the functions.php file. For example, a custom code may need additional scripts and stylesheets to work properly.

并非您需要的所有功能和特性都可以添加到functions.php文件中。 例如,自定义代码可能需要其他脚本和样式表才能正常工作。

We believe that the theme’s functions.php file should be used for what it’s intended purpose was: theme based functionality added by developers.

我们认为主题的functions.php文件应用于其预期目的:开发人员添加的基于主题的功能。

安装WordPress插件的利与弊 (Pros and Cons of Installing WordPress Plugins)

WordPress plugins

WordPress plugins are like apps for your WordPress website. They contain code that hooks itself to the core WordPress software to provide more features and functionality.

WordPress插件就像您的WordPress网站的应用程序。 它们包含与WordPress核心软件挂钩的代码,以提供更多功能。

See our article on what are WordPress plugins and how do they work.

请参阅有关什么是WordPress插件以及它们如何工作的文章

Here are some advantages of using a WordPress plugin instead of adding code to your functions file.

这里是使用WordPress插件而不是向函数文件添加代码的一些优点。

Pros

优点

  • A WordPress plugin doesn’t depend on your theme

    WordPress插件不取决于您的主题
  • If it is the same code, then it would have the same performance impact as functions file

    如果是相同的代码,那么它将与功能文件具有相同的性能影响。
  • It is easier to disable, update, or reinstall as needed

    根据需要更容易禁用,更新或重新安装
  • Easier to manage as you know what each plugin does from its description

    通过其说明了解每个插件的功能,使其更易于管理

Now, we are not saying that installing all WordPress plugins is good. There are some disadvantages as well.

现在,我们并不是说安装所有WordPress插件是件好事。 也有一些缺点。

Cons

缺点

  • You will have to install updates for another plugin

    您将必须安装另一个插件的更新
  • Plugin author could abandon the plugin in the future specially if it’s free

    如果免费的话,插件作者将来可能会放弃该插件
  • You wouldn’t get to practice with the code

    您不会去练习代码
WordPress插件与函数文件–哪个更好? (WordPress Plugin vs Functions File – Which One is Better?)

If there is a WordPress plugin with the same code, then we believe that using a plugin is a better choice.

如果有一个具有相同代码的WordPress插件,那么我们认为使用插件是一个更好的选择。

In terms of performance, it does not matter where the code runs. Whether it loads from functions.php file or as a separate plugin, if it is the same code, then it will have the same performance impact.

在性能方面,代码在哪里运行都无关紧要。 无论是从functions.php文件加载还是作为单独的插件加载,如果它是相同的代码,那么它将对性能产生相同的影响。

Managing separate plugins is much easier than editing a single large functions file. Adding random code snippets in the same file even when they are not related or dependant on each other makes things complicated.

管理单独的插件比编辑单个大型函数文件容易得多。 即使在彼此不相关或不相互依赖的情况下,在同一文件中添加随机代码段也会使事情变得复杂。

If you choose a plugin, then you can benefit from future updates which may improve performance, fix bug, or patch security vulnerability.

如果选择插件,则可以从将来的更新中受益,这些更新可能会提高性能,修复错误或修补程序安全漏洞。

When Should I Add Code to Functions.php File?

什么时候应该将代码添加到Functions.php文件中?

If the functionality you are trying to add is not available as a plugin and the code snippet is really simple, then you can add it to your theme’s functions file.

如果您要添加的功能无法作为插件使用,并且代码段非常简单,则可以将其添加到主题的功能文件中。

Another possible scenario is when you are adding functionality that is related to your WordPress theme or child theme. For example, creating new image sizes, showing dates, adding custom code before or after content, and more.

另一种可能的情况是,当您添加与WordPress主题或子主题相关的功能时。 例如, 创建新的图像大小 ,显示日期,在内容之前或之后添加自定义代码等等。

If you often add custom code snippets to your website, then there are even better ways to do that. You can save them in a site-specific WordPress plugin or use Code Snippets plugin to organize all your custom code.

如果您经常在网站上添加自定义代码段,那么还有更好的方法可以做到这一点。 您可以将它们保存在特定站点的WordPress插件中,也可以使用Code Snippets插件来组织所有自定义代码。

We hope this article helped you understand WordPress plugin vs functions.php file and which one is better. You may also want to see our beginner’s guide on how to choose the best WordPress plugins for your website.

我们希望本文能帮助您了解WordPress插件与functions.php文件,哪个更好。 您可能还希望查看我们的初学者指南,以了解如何为您的网站选择最佳的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/beginners-guide/wordpress-plugin-vs-functions-php-file-which-is-better/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值