wordpress插件开发
The official WordPress.org Plugin Directory is home to an amazing collection of useful plugins, it’s usually the first place WordPress users look when seeking to extend the functionality of their sites. The Plugin Directory offers great opportunities for developers, however there are some guidelines you need to follow. A base plugin will help you to easily and quickly get started in the development process.
官方WordPress.org插件目录包含大量有用的插件,通常是WordPress用户在寻求扩展其网站功能时的首选。 插件目录为开发人员提供了绝佳的机会,但是您需要遵循一些准则。 一个基本的插件将帮助您轻松快速地开始开发过程。
These are the necessary files and directories for every plugin that you want to publish to the WordPress plugin directory. These files and directories represent your base plugin. The current plugin version and all future releases will be packed into one single directory i.e. plugin-name.
这些是您要发布到WordPress插件目录的每个插件的必需文件和目录。 这些文件和目录代表您的基本插件 。 当前的插件版本和所有将来的发行版将打包到一个目录中,即plugin-name 。
--plugin-name --assets -screenshot-n.png -icon-256x256.png -banner-772x250.png --trunk --admin --css --js --inc -admin.php --public --css --js --inc -public.php --inc -activation.php -deactivation.php --languages -plugin-name.pot -plugin-name.php -uninstall.php -README.txt --tagsLet’s now review the use of each of these directories and files. We’ll also look at the code we need to put inside the files.
现在,让我们回顾一下这些目录和文件的用法。 我们还将查看需要放入文件中的代码。
This directory contains images of the plugin i.e. plugin screenshots, banner image for the plugin page on WordPress.org and an icon.
此目录包含插件的图像,即插件屏幕截图,WordPress.org上插件页面的横幅图像和图标。
You can provide any number of screenshots. The screenshots can be any of following formats: png, jpg, jpeg or gif. The screenshot must also show the latest release of your plugin. Suppose you have five screenshots, then your assets directory should look like this:
您可以提供任意数量的屏幕截图。 屏幕截图可以是以下任何格式:png,jpg,jpeg或gif。 屏幕截图还必须显示插件的最新版本。 假设您有五个屏幕截图,那么您的资产目录应如下所示:
--assets -screenshot-1.png -screenshot-2.png -screenshot-3.png -screenshot-4.png -screenshot-5.png -icon-256x256.png -banner-772x250.pngIcons must be 256×256 pixels and a banner must be 772×250 pixels. It’s not compulsory to provide screenshots, an icon or a banner, however, providing them is recommended since it does increase engagement with your plugin.
图标必须为256×256像素,横幅必须为772×250像素。 不一定要提供屏幕截图,图标或横幅,但建议您提供它们,因为它会增加与插件的互动。
Trunk is your working directory. Here is you’ll write your code. When you want to publish your plugin you’ll need to copy the contents from the trunk directory to a new version directory within the tags directory. The version directory represents the current version of the plugin code.
Trunk是您的工作目录。 这是您编写代码的地方。 当您要发布插件时,需要将内容从主干目录复制到标签目录中的新版本目录。 版本目录表示插件代码的当前版本。
When you’ve finished creating your new plugin, you’ll want publish it with a version 1.0. Then your directories will look like this:
完成创建新插件后,您需要使用1.0版进行发布。 然后您的目录将如下所示:
--plugin-name --assets -screenshot-n.png -icon-256x256.png -banner-772x250.png --trunk --admin --css --js --inc -admin.php --public --css --js --inc -public.php --inc -activation.php -deactivation.php --languages -plugin-name.pot -plugin-name.php -uninstall.php -README.txt --tags --1.0 --admin --css --js --inc -admin.php --public --css --js --inc -public.php --inc -activation.php -deactivation.php --languages -plugin-name.pot -plugin-name.php -uninstall.php -README.txtThe tags/1.0 directory will contain all of the same code as what’s in the current trunk directory.
tags/1.0目录将包含与当前主干目录相同的所有代码。
Now, if you want to modify trunk and release a new version of your plugin as 2.0, then create a new directory tags/2.0 and copy all of the latest code from the trunk directory to this new directory. Therefore your trunk directory should always reflect the current changes. Now your directories would look something like this:
现在,如果要修改主干并以2.0发行新版本的插件,请创建一个新目录tags/2.0 ,并将所有最新代码从主干目录复制到此新目录。 因此,您的主干目录应始终反映当前的更改。 现在您的目录如下所示:
--plugin-name --assets -screenshot-n.png -icon-256x256.png -banner-772x250.png --trunk --admin --css --js --inc -admin.php --public --css --js --inc -public.php --inc -activation.php -deactivation.php --languages -plugin-name.pot -plugin-name.php -uninstall.php -README.txt --tags --1.0 --admin --css --js --inc -admin.php --public --css --js --inc -public.php --inc -activation.php -deactivation.php --languages -plugin-name.pot -plugin-name.php -uninstall.php -README.txt --2.0 --admin --css --js --inc -admin.php --public --css --js --inc -public.php --inc -activation.php -deactivation.php --languages -plugin-name.pot -plugin-name.php -uninstall.php -README.txtUsers download the content inside the version directories, not the trunk directory.
用户将内容下载到版本目录中,而不是中继目录中。
The README.txt file is the primary source of information displayed for the plugin in the WordPress.org plugins directory. It should be present and should clearly explain how to use the plugin and what the plugin does, even if you think it’s obvious.
README.txt文件是在WordPress.org插件目录中显示的该插件的主要信息来源。 它应该存在并且应该清楚地解释如何使用该插件以及该插件的作用,即使您认为它很明显也是如此。
A README.txt file is present in the trunk directory and also in the version directories of tags directory.
README.txt文件存在于主干目录以及标签目录的版本目录中。
This is sample content of README.txt file for our base plugin. This file is written in markdown.
这是基本插件的README.txt文件的示例内容。 该文件以markdown编写。
=== Plugin Name === Contributors: (this should be a comma separated list of wordpress.org userid's of this plugin developers) Donate link: http://example.com/ Tags: tag1, tag2, tag3 Requires at least: 3.0.1 Tested up to: 3.4 Stable tag: 2.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html Here is a short description of the plugin. This should be no more than 150 characters. No markup here. == Description == This is the long description. No limit, and you can use Markdown (as well as in the following sections). == Installation == This section describes how to install the plugin and get it working. == Frequently Asked Questions == = This is question 1 = This is answer to question 1 = This is question 2 = This is answer to question 2 == Screenshots == 1. This is description for our first screenshot i.e., screenshot-1.png. 2. This is description for our second screenshot i.e., screenshot-2.png. 3. This is description for our third screenshot i.e., screenshot-3.png. 4. This is description for our fourth screenshot i.e., screenshot-4.png. 5. This is description for our fifth screenshot i.e., screenshot-5.png. == Changelog == = 3.0 = * A change since the previous version. * Another change. = 2.0 = * A change since the previous version. * Another change. == Upgrade Notice == = 3.0 = Upgrade notices describe the reason a user should upgrade. No more than 300 characters. = 2.0 = This version fixes a security related bug. Upgrade immediately.Most things in the README.txt file are self explanatory.
README.txt文件中的大多数内容都是自解释性的。
One of the things you do need to understand is the Stable tag:. The Stable tag of README.txt file of the trunk directory should point to the name of the latest version directory. This is how the WordPress plugin directory knows the latest and most stable release of the plugin. When you create a new version of your plugin you need to change trunk directory’s README.txt file’s Stable tag value to the new version directory. The Stable tag of README.txt file in the tags version directories should point to the name of that particular version directory.
您需要了解的一件事是Stable tag: 。 中继目录的README.txt文件的“稳定”标记应指向最新版本目录的名称。 这就是WordPress插件目录如何知道插件的最新,最稳定的版本。 创建插件的新版本时,需要将中继目录的README.txt文件的“稳定”标记值更改为新版本目录。 标记版本目录中的README.txt文件的稳定标记应指向该特定版本目录的名称。
The README.txt file in the version directories represent each of the versions, and README.txt in the trunk directory represents the latest version release and therefore is same as the current release version directory’s README.txt file.
版本目录中的README.txt文件表示每个版本,而主干目录中的README.txt表示最新版本,因此与当前发行版本目录的README.txt文件相同。
Not everyone that uses WordPress speaks English. Therefore you don’t want to prevent non-English speaking users from using your plugin. You can avoid this by translating all your plugin’s displayed strings to different languages.The process of making your plugin compatible for multiple languages is called as Internationalization (i18n). A .pot file contains alternative language versions of your plugin’s strings. Generating a .pot file for your plugin is called Localization (L10n). At SitePoint we’ve covered i18n and L10n in WordPress, something definitely worth checking out if you’re interested.
并非每个使用WordPress的人都说英语。 因此,您不想阻止非英语用户使用您的插件。 您可以通过将所有插件显示的字符串翻译成不同的语言来避免这种情况。使插件兼容多种语言的过程称为国际化 (i18n)。 .pot文件包含插件字符串的替代语言版本。 为您的插件生成.pot文件称为本地化 (L10n)。 在SitePoint,我们已经用WordPress涵盖了i18n和L10n ,如果您有兴趣的话,绝对值得一试。
This language file (.pot) is not loaded automatically, you need to load it using the load_plugin_textdomain function. To do this, place this code in your plugin’s main file i.e. plugin-name.php file.
此语言文件(.pot)不会自动加载,您需要使用load_plugin_textdomain函数加载它。 为此,请将此代码放置在插件的主文件(即plugin-name.php文件)中。
load_plugin_textdomain("plugin-name", false, basename(dirname(_FILE_)), "/languages");inc/activation.php and inc/deactivation.php files contain code that is executed when the plugin is activated or deactivated respectively.
inc/activation.php和inc/deactivation.php文件包含分别在激活或停用插件时执行的代码。
Now we need to load inc/activation.php and inc/deactivation.php files only during plugin activation and deactivation respectively. We can do this by placing the below code in plugin-name.php file.
现在,我们仅在插件激活和停用期间分别需要加载inc/activation.php和inc/deactivation.php文件。 我们可以通过将以下代码放在plugin-name.php文件中来实现。
function plugin_activated() { require_once "inc/activation.php"; } function plugin_deactivated() { require_once "inc/deactivation.php"; } register_activation_hook(__FILE__, "plugin_activated"); register_deactivation_hook(__FILE__, "plugin_deactivated");You can learn more about activation and deactivation hooks here.
您可以在此处了解有关激活和停用挂钩的更多信息。
This file is executed when a plugin is deleted by the user. Put this content inside the uninstall.php file
当用户删除插件时,将执行此文件。 将此内容放入uninstall.php文件中
//this check makes sure that this file is not called manually. if (!defined("WP_UNINSTALL_PLUGIN")) exit(); //put plugin uninstall code hereInside admin/admin.php is where you’re supposed to put the admin panel dashboard specific functionality of the plugin. Then, inside public/public.php, is where you’re supposed to put the public facing functionality of the plugin.
您应该在admin/admin.php内部放置管理面板仪表板特定于插件的功能 。 然后,在public/public.php内部,应该放置该插件的面向公众的功能 。
Your whole plugin is actually a SVN repository on the WordPress.org plugin directory cloud. Once you submit your plugin and it’s approved, you will have a SVN repository on the cloud to which you can make changes or add new version releases.
您的整个插件实际上是WordPress.org插件目录云上的SVN存储库 。 提交插件并获得批准后,您将在云上拥有一个SVN存储库,可以在其中进行更改或添加新的版本。
To make changes or add releases to the remote repository you need to create a local copy of the repository using SVN. Then, make changes to the local copy and then commit it to the remote repository using SVN.
要对远程存储库进行更改或添加发布,您需要使用SVN创建存储库的本地副本。 然后,对本地副本进行更改,然后使用SVN将其提交到远程存储库。
You can refer to WordPress.org plugin directory’s FAQ for your common questions. You can use README.txt validator to validate your README.txt file. You can also generate a README.txt file using the Plugin Readme Generator at GenerateWP.
您可以参考WordPress.org插件目录的常见问题解答。 您可以使用README.txt验证程序来验证README.txt文件。 您还可以使用GenerateWP上的插件自述生成器来生成README.txt文件。
翻译自: https://www.sitepoint.com/creating-a-base-plugin-for-wordpress-org-plugin-directory/
wordpress插件开发