您如何组织插件和主题代码?

发布于 2024-09-10 04:29:52 字数 269 浏览 4 评论 0原文

我开始使用 WordPress 作为 CMS,现在 V3 可以更轻松地管理分类法和自定义帖子类型。我的工作主要集中在开发插件和主题。

我最大的插件做了一些管理工作(添加管理菜单项以及相关页面和功能),但也做了一些导入和导出,并挂钩一些基本的后期处理处理(“创建新帖子时”)。

我最大的主题非常小,它所做的只是以自定义方式显示自定义帖子。

经过几周的工作,我已经有了几千个 LoC,而且挖掘起来越来越难。这引出了以下问题:您如何组织 WP 插件代码?那么你的 WP 主题代码呢?

I starting working with WordPress as a CMS, now that the V3 makes it way easier to manage taxonomies and custom post types. My work is mostly focused on developing plugins and themes.

My biggest plugin does some admin stuff (add admin menu items and the related pages and features), but also does some importing and exporting, and hooks some of the base post processing treatments ("when a new post is created").

My biggest theme is pretty small, and all it does is display custom posts in a custom way.

After a few weeks of work, I have several thousands of LoC, and it's getting harder and harder to dig into it. Which leads me to the following question: How do you organize your WP plugins code? And what about your WP themes code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过气美图社 2024-09-17 04:29:52

数千个LoC

这真是史诗般的!我一直发现 WP 的美妙之处在于我可以,正如 jQuery 所说;

少写,多做!

您最好将 Pods CMS 与 WP 一起使用来减少代码。

several thousands of LoC

That's pretty epic! I've always found the beauty of WP is that I can, as jQuery put it;

Write less, do more!

You might be much better off using Pods CMS alongside WP to cut down your code.

请叫√我孤独 2024-09-17 04:29:52

这就是我们构建包括主题、第三方插件和自定义代码的客户端部署的方式。

  • wp-content/plugins 仅包含第三方插件,此处未修改任何代码,并且该网站不应因禁用/删除任何这些插件而截止。
  • wp-content/themes 应包含与前端呈现相关的代码。诀窍不是不使用与演示不直接相关的代码来重载主题(functions.php 和其他与主题相关的文件)。
  • mu-plugins/ 包含所有特定于实现的业务逻辑。这里的东西永远不应该被禁用,并且是操作所必需的。

这是一个非常简短的总结,但简而言之,这是我们发现最能防止失败的代码的逻辑划分。

This is how we structure client deployments that include themes, third-party plugins, and custom code.

  • wp-content/plugins only contains third-party plugins, no code in here is modified, and the site should not be deadlined by any of these plugins being disabled / removed.
  • wp-content/themes should contain the code related to presentation of the front-end. The trick is not not overload the theme (functions.php and other theme-related files) with code not directly related to presentation.
  • mu-plugins/ contains all of your implementation-specific business logic. Things in here should never be disabled, and are required for operations.

That is avery brief summary, but in a nutshell that is the logical compartmentalization of code that we've found to be most failure proof.

春风十里 2024-09-17 04:29:52

为什么不将插件按功能拆分成多个文件?主题也是如此。你对此有什么问题吗?

Why not to split plugin into several files by function? The same goes to themes. Any problem you have with that?

哀由 2024-09-17 04:29:52

基本上有三种方法可以做到这一点: 前缀函数,使用 require_once 按功能包含文件,这很常见。

另一种被广泛吹捧的“正确”方法是拥有一个包含所有插件代码的巨型类。正如您所说,在保持良好状态的同时,一旦该文件进入数千行代码,您就会陷入可管理性的困境。

我喜欢的方式是来自我的 C# 背景的一种方式 - 有一个主插件类和其他“worker”类(您可以将它们放在命名空间中以保持类名简短)。例如,有一个专门用于管理部分的类(它也可以有自己的小“子类”,例如每个页面一个)。将所有这些代码重构到不同的类和文件中需要一段时间,但这是值得的 - 跟踪所有内容以及让人们在代码库上工作会更容易一起。它还迫使您更多地考虑应用程序如何组合在一起,这有助于更好地编写代码。

我实际上正在写一篇关于此的文章系列,概述了三种不同的方法。您可以在此处查看第一部分。接下来几周还会有另外两个。

希望我有帮助!

There are basically three ways you can do this: prefixed functions, with require_once's including files by functionality, which is quite common.

The other "right" way that's touted a lot is having one giant class with all your plugin code in it. While keeping things nice, as you said, once that file gets into the thousands of lines of code, you're screwed for manageability.

The way I like to do it is a way coming from my C# background - have one main plugin class, and other "worker" classes (you can put them in a namespace to keep classnames short). For example, have a class dedicated to the admin section (it can have its own little "subclasses" too, say one for each page). It'll take a while to refactor all this code into the different classes and files, but it'll be worth it - it'll be much easier to keep track of everything, as well as for example getting people to work on the codebase together. It also forces you to think more of how your application all fits in together, which lends to better thought out code.

I'm actually writing an article series about this, outlining the three different methods. You can take a look at the first instalment here. There are two more coming in the following weeks.

Hope I helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文