wordpress插件开发

tech2022-11-25  90

wordpress插件开发

A low learning curve into WordPress plugin development means that there is no one definitive way to build a plugin. A plugin can be as simple as a single file like Hello Dolly, or it can be structured as complex as needed to cater for various requirements and functionality. The WordPress Plugin Boilerplate aims to provide a standardized, high quality foundation to build your next awesome plugin.

WordPress插件开发的学习曲线很低,这意味着没有一种确定的方式来构建插件。 插件可以像Hello Dolly这样的单个文件一样简单,也可以根据需要满足各种要求和功能,使其结构复杂。 WordPress插件样板旨在提供一个标准化,高质量的基础来构建您的下一个很棒的插件。

In this first part of the series, we’ll take a deep look into the boilerplate, including how the files and folders are structured as well as code organization of the boilerplate.

在本系列的第一部分中,我们将深入了解样板,包括文件和文件夹的结构以及样板的代码组织。

背景 (Background)

WordPress plugin boilerplate is originally an initiative by Tom McFarlin to provide a standardized, object-oriented approach to building WordPress plugins. Since the boilerplate is built on top of the Plugin API, and following the Coding Standards and Documentation Standards recommended by WordPress, you can be sure that you’re in safe hands.

WordPress插件样板最初是Tom McFarlin提出的一项倡议,旨在提供标准化的,面向对象的方法来构建WordPress插件。 由于样板是在Plugin API的基础上构建的,并且遵循WordPress建议的编码标准和文档标准 ,因此您可以确保一切安全。

Recent update of the boilerplate to version 3.0.0 brings major improvements to the boilerplate itself, in terms of code organisation, as well as further separation of concerns within your plugin. If you’re already familiar developing your plugin using the previous version of boilerplate (v2.6.2), you might need to take some time to relearn what’s new in the latest version, which includes new folder structure.

样板的最新更新到版本3.0.0,在代码组织以及插件内关注点的进一步分离方面,对样板本身进行了重大改进。 如果您已经熟悉使用以前版本的样板(v2.6.2)开发插件,则可能需要花一些时间来重新学习最新版本的新功能,其中包括新的文件夹结构。

This boilerplate is no longer maintained by Tom McFarlin himself (at the time of writing), he’s passed the torch to Devin Vinson. The development is still continuing as usual, so if you have any issues, feel free to report one at the GitHub repository. Contributions are also definitely welcomed.

Tom McFarlin本人(在撰写本文时)已不再维护此样板,他已将火炬传递给了Devin Vinson。 开发仍然像往常一样继续进行,因此,如果您有任何问题,请随时在GitHub存储库中 报告 。 也绝对欢迎捐款。

获得副本 (Getting Yourself a Copy)

The simplest way to get a copy of WordPress plugin boilerplate is by cloning the git repository itself.

获得WordPress插件样板的最简单方法是克隆git存储库本身。

git clone https://github.com/DevinVinson/WordPress-Plugin-Boilerplate.git

Or you can always download a zip copy from the GitHub repository.

或者,您始终可以从GitHub存储库下载zip副本。

Since version 3.0.0 was released, the plugin cannot be simply copied into your wp-content/plugins directly. This is because the actual source code of the plugin is not contained in the root directory, but actually resides in the subdirectory called trunk. Don’t worry about this now, we’ll talk about the new folder structure later on in this article.

由于版本3.0.0已发布,因此无法将wp-content/plugins直接复制到您的wp-content/plugins 。 这是因为插件的实际源代码未包含在根目录中,而是实际位于名为trunk的子目录中。 现在不用担心,我们将在本文后面讨论新的文件夹结构。

Full detailed instructions on how to install the boilerplate correctly is described in the README.md file. In a nutshell, you can either:

README.md文件中描述了有关如何正确安装样板的完整详细说明。 简而言之,您可以:

Copy the trunk directory into the wp-content/plugins folder OR

将trunk目录复制到wp-content/plugins文件夹中,或者

Copy the whole boilerplate directory, and then create a symbolic link for the trunk subdirectory to the wp-content/plugins/<plugin-name>/. Creating the symbolic link will depends on the operating system that you’re using.

复制整个样板目录,然后为trunk子目录创建符号链接到wp-content/plugins/<plugin-name>/ 。 创建符号链接将取决于您使用的操作系统。

Once you’ve done that, you should now find a plugin called WordPress Plugin Boilerplate in your plugin list in the admin dashboard, assuming you haven’t made any modifications to the boilerplate itself. Simply activate the plugin and you’re good to go!

完成此操作后,假设您未对样板本身进行任何修改,现在您应该在管理控制台的插件列表中找到一个名为WordPress Plugin Boilerplate的插件。 只需激活插件,您就可以使用了!

使用在线生成器自定义插件 (Using the Online Generator to Customize the Plugin)

Once activated, you’ll have a sort of vanilla plugin that doesn’t really do anything – that is, just yet. You’ll also need to run a search and replace to the whole boilerplate’s codebase, and this process can be very tedious and time consuming. Luckily, there is an unofficial generator built by Enrique Chavez that can automate the process. Simply head to http://wppb.me/ and complete the form located at the bottom of the page to generate a personalized copy of WordPress plugin boilerplate.

激活后,您将拥有某种vanilla插件,实际上并没有做任何事情。 您还需要搜索并替换整个样板代码库,此过程可能非常繁琐且耗时。 幸运的是,有一个由恩里克·查韦斯(Enrique Chavez)构建的非官方生成器 ,可以自动执行该过程。 只需访问http://wppb.me/并完成页面底部的表单即可生成WordPress插件样板的个性化副本。

资料夹结构 (Folder Structures)

Let’s take a closer look on how the WordPress plugin boilerplate version 3.0.0 is organised compared to the old one. As mentioned previously, the actual plugin code is contained in a subdirectory called trunk instead of in the root directory.

与旧版本相比,让我们仔细研究一下WordPress插件样板版本3.0.0的组织方式。 如前所述,实际的插件代码包含在一个名为trunk的子目录中,而不是在根目录中。

This is to follow the standards employed in the WordPress official plugin repository which contains three main directories which are assets, branches and trunk. The boilerplate already provides two of those, assets and trunk.

这将遵循WordPress官方插件存储库中采用的标准,该存储库包含三个主要目录,即assets , branches和trunk 。 样板文件已经提供了其中两个, assets和trunk 。

Here’s a complete directory and files contained in the boilerplate, at the time of writing:

在编写本文时,这是样板中包含的完整目录和文件:

|-- plugin-name/ | |-- assets/ | | |-- banner-772x250.png | | |-- icon-256x256.png | | |-- screenshot-1.png | | | |-- trunk/ | |-- admin/ | | |-- css/ | | | |-- plugin-name-admin.css | | |-- js/ | | | |-- plugin-name-admin.js | | |-- partials/ | | | |-- plugin-name-admin-display.php | | | | | |-- class-plugin-name-admin.php | | |-- index.php | | | |-- includes/ | | |-- class-plugin-name-activator.php | | |-- class-plugin-name-deactivator.php | | |-- class-plugin-name-i18n.php | | |-- class-plugin-name-loader.php | | |-- class-plugin-name.php | | |-- index.php | | | |-- languages/ | | |-- plugin-name.pot | | | |-- public/ | | |-- css/ | | | |-- plugin-name-public.css | | |-- js/ | | | |-- plugin-name-public.js | | |-- partials/ | | | |-- plugin-name-public-display.php | | | | | |-- class-plugin-name-public.php | | |-- index.php | | | |-- LICENSE.txt | |-- README.txt | |-- index.php | |-- plugin-name.php | |-- uninstall.php | |-- .gitignore |-- CHANGELOG.md |-- README.md

Here’s a little explanation of what each file and folder does:

这是每个文件和文件夹的作用的一些解释:

.gitignore

.gitignore

Provides a sane default .gitignore to most of the stuff that should not exist in your git repository.

提供合理的默认.gitignore到git存储库中不应该存在的大多数内容。

CHANGELOG.md

CHANGELOG.md

Standard changelog of changes to the boilerplate with date of change.

标准样板的变更记录以及变更日期。

README.md

README.md

A useful starting guide that lists the installation instructions, among other few other sections such as tools recommendations and credits.

有用的入门指南,列出了安装说明,以及其他一些小节,例如工具建议和信用。

assets

assets

This directory contains the recommended resources that you need to provide whenever you decide to publish your plugin to the WordPress plugin repository. All of the images contained in this directory is the recommended resolution for publishing.

该目录包含建议的资源,无论何时决定将插件发布到WordPress插件存储库,都需要提供这些资源。 建议使用此目录中包含的所有图像进行发布。

trunk

trunk

This is the actual plugin that you are going to develop. There are a few folders that separate the codebase between admin and public facing functionality. We’ll go through in details of what each subdirectory represents.

这是您将要开发的实际插件。 有一些文件夹将管理和面向公众的功能之间的代码库分开。 我们将详细介绍每个子目录所代表的内容。

admin

admin

There are three directories contained within the admin directory, namely css, js and partials. As its name suggests, all admin facing functionality should be placed here. By default, the plugin-name-admin.js and plugin-name-admin.css is enqueued to your wp-admin. class-plugin-name-admin.php will provide generic functionality where you can define your admin specific hooks.

admin目录中包含三个目录,即css , js和partials 。 顾名思义,所有面向管理员的功能都应放在此处。 默认情况下, plugin-name-admin.js和plugin-name-admin.css排队到您的wp-admin 。 class-plugin-name-admin.php将提供通用功能,您可以在其中定义特定于管理员的挂钩。

public

public

This directory is pretty much similar to what the admin directory has to offer, the only difference is that the public directory should be used to store all of your public facing functionality codebase.

该目录与admin目录必须提供的目录非常相似,唯一的区别是public目录应用于存储所有面向公众的功能代码库。

languages

languages

A starting .pot file where you can provide translation functionality with your plugin.

一个起始.pot文件,您可以在其中为插件提供翻译功能。

includes

includes

This is probably where pretty much all the magic happens. There are five starting classes included by default, which we will discuss later in the next section.

这可能是几乎所有魔术发生的地方。 默认情况下,包括五个开始类,我们将在下一节稍后讨论。

LICENSE.txt

LICENSE.txt

A copy of GPL v2 license is included by default.

默认情况下包含GPL v2许可证的副本。

README.txt

README.txt

A starting point for your plugin README file. This file pretty covers all of the sections that you can further fill in to provide a good plugin page on WordPress plugin repository.

插件自述文件的起点。 该文件涵盖了所有部分,您可以进一步填写这些部分以在WordPress插件存储库上提供一个不错的插件页面。

plugin-name.php

plugin-name.php

The point of entry for your plugin. In here, a general plugin file header is included that you can modify to your own liking. The register_activation_hook and register_deactivation_hook are also registered in this file if you ever need to include some sort of functionality on plugin activation and/or deactivation.

插件的入口点。 在这里,包含了一个通用的插件文件头,您可以根据自己的喜好对其进行修改。 如果您需要在插件激活和/或停用时包含某种功能,则register_activation_hook和register_deactivation_hook也会在此文件中注册。

包含的类 (Included Classes)

As mentioned before, there are five default classes provided inside the trunk/includes directory. Let’s see what each of these do:

如前所述, trunk/includes目录中提供了五个默认类。 让我们看看它们各自的作用:

class-plugin-name-activator.php

class-plugin-name-activator.php

This class is instantiated during the plugin activation. It only has one static method, activate() which is registered to the register_activation_hook. Use this class whenever you find yourself in need to do something on plugin activation such as creating custom tables or saving default options.

此类在插件激活期间实例化。 它只有一个静态方法activate() ,该方法已注册到register_activation_hook 。 每当您发现需要在插件激活上执行某些操作(例如创建自定义表或保存默认选项)时,请使用此类。

class-plugin-name-deactivator.php

class-plugin-name-deactivator.php

The counterpart for class-plugin-name-deactivator.php. It also only contains one static method, deactivate() which can be used to run any functionality during plugin deactivation.

对应于class-plugin-name-deactivator.php 。 它还仅包含一个静态方法deactivate() ,可用于在插件停用期间运行任何功能。

class-plugin-name-i18n.php

class-plugin-name-i18n.php

A starting point for i18n functionality of your plugin. It has one property, $domain which stores your plugin text domain. This property can be set using the public method set_domain(). Finally, the method load_plugin_textdomain() in this class will be called whenever the plugin is loaded.

插件的i18n功能的起点。 它有一个属性, $domain ,用于存储您的插件文本域。 可以使用公共方法set_domain()设置此属性。 最后,每当加载插件时,将调用此类中的方法load_plugin_textdomain() 。

class-plugin-name-loader.php

class-plugin-name-loader.php

Probably the most important class in the boilerplate. It contains two properties, $actions and $filters where all the hooks registered in the plugin will be stored. It provides two simple wrapper functions, add_action() and add_filter that are used to add actions or filters into the $actions and $filters properties. This should not be confused with the WordPress default add_action() and add_filter() function as this class does not actually register them directly. All of the hooks will only be registered during another method called run().

可能是样板中最重要的一类。 它包含两个属性, $actions和$filters ,将存储在插件中注册的所有钩子。 它提供了两个简单的包装函数,即add_action()和add_filter ,用于将操作或过滤器添加到$actions和$filters属性中。 请勿将其与WordPress默认的add_action()和add_filter()函数混淆,因为此类实际上并未直接注册它们。 所有挂钩都只会在另一个名为run()方法中注册。

class-plugin-name.php

class-plugin-name.php

The class that glues all the pieces together. It holds important information about the plugin such as the plugin name and version. Plus, it will load the dependencies using the method load_dependencies() which will include all the above four classes and plugin text domain will be set using set_locale() method. All admin and public hooks that were previously registered can also be defined here.

该类将所有片段粘合在一起。 它包含有关插件的重要信息,例如插件名称和版本。 另外,它将使用方法load_dependencies()加载依赖项,该方法将包括上述所有四个类,并且插件文本域将使用set_locale()方法设置。 先前已注册的所有admin和public钩子也可以在此处定义。

This class also provides simple get methods, such as get_plugin_name() to return the plugin name, get_version() to return the current plugin version and get_loader() that keeps an instance of class-plugin-name-loader.php.

此类还提供了简单的get方法,例如get_plugin_name()返回插件名称, get_version()返回当前插件版本,以及get_loader() ,该实例保留了class-plugin-name-loader.php 。

结论 (Conclusion)

In short, the WordPress plugin boilerplate provides a terrific starting point for plugin developers. It follows the recommended WordPress coding standards as well as WordPress documentation standards so you know you’re starting off on the right foot. Besides, publishing to the WordPress plugin repository is made easier since the boilerplate already provides much of what you’ll need.

简而言之,WordPress插件样板为插件开发人员提供了一个了不起的起点。 它遵循推荐的WordPress编码标准以及WordPress文档标准,因此您知道自己是从右脚开始的。 此外,由于样板已经提供了您需要的很多内容,因此发布到WordPress插件存储库变得更加容易。

Once we’re familiar with the code organisation and folder structure, we’ll explore how to develop an actual plugin using the WordPress plugin boilerplate in the second part of the series.

一旦我们熟悉了代码组织和文件夹结构,我们将在本系列的第二部分中探索如何使用WordPress插件样板开发实际的插件。

翻译自: https://www.sitepoint.com/wordpress-plugin-boilerplate/

wordpress插件开发

相关资源:wp-reactivate:针对WordPress插件的样板-源码
最新回复(0)