wordpress插件开发
Are you looking for an introduction to WordPress plugin development that will discuss all of the important steps and hold your hand along the way? If so, you’re in luck. This tutorial will outline the basics of what you need to know about designing your own plugins and some pointers on best practices.
您是否正在寻找WordPress插件开发的介绍,该讨论将讨论所有重要步骤并一路过关斩将? 如果是这样,那么您很幸运。 本教程将概述您在设计自己的插件时需要了解的基础知识,以及一些有关最佳做法的指针。
![WordPress Plugin Development](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2016/01/1452611845WordPress-Plugin-Development.jpg)
什么是WordPress插件? (What Are WordPress Plugins?)
Plugins are one of WordPress’ most powerful assets. In essence, plugins are modules you activate on your website to provide a series of features or elements.
插件是WordPress最强大的资产之一。 本质上,插件是您在网站上激活以提供一系列功能或元素的模块。
The functionality you can add to your website depends on what each specific plugin has been created to do. There are a wide selection of plugins, ranging from simple plugins (such as ones that add styling or small theme changes) all the way to extensive plugins (that provide significant changes such as eCommerce integrations or third party connections).
您可以添加到网站的功能取决于每个特定插件的创建方式。 插件的选择范围很广,从简单的插件(例如添加样式或小的主题更改的插件)一直到扩展的插件(提供重大更改,例如电子商务集成或第三方连接)。
Plugins are different from your theme and work independently, using hooks, filters, shortcode, widgets and custom code to perform their functionality.
插件与您的主题不同,并且可以独立使用钩子,过滤器,短代码,小部件和自定义代码来执行其功能。
优点,缺点和注意事项 (Strengths, Weaknesses & Considerations)
Plugins are great, they provide both developers and admins with a way to extend and re-use functionality. While they are a great tool they do still have both their strengths and weaknesses.
插件很棒,它们为开发人员和管理员提供了一种扩展和重用功能的方法。 尽管它们是一个很好的工具,但是它们仍然具有优点和缺点。
长处 (Strengths)
- You can add plugins with almost any theme and expect it to work correctly. Plugins were created to be modular so that you can copy into a new project (or distribute) which is great. 您可以添加几乎任何主题的插件,并期望它能正常工作。 插件的创建是模块化的,因此您可以将其复制到新项目(或分发)中,这很棒。
- A plugin acts as a container for all of your code and elements. If you were to build similar functionality in the theme you might get the elements mixed up. With a plugin, everything that needs to function exists within a single package. 插件充当所有代码和元素的容器。 如果要在主题中构建类似的功能,则可能会混淆元素。 使用插件,所有需要运行的功能都存在于单个程序包中。
- They are easy to make and can take advantage of WordPress’s action and filter hooks. 它们易于制作,可以利用WordPress的操作和过滤器挂钩。
- Can be created easily in an Object Orientated fashion, creating more robust and manageable code. 可以以面向对象的方式轻松创建,从而创建更健壮和可管理的代码。
弱点 (Weaknesses)
- Limited access to the website’s theme. Plugins can’t output code directly into a location on a theme. A theme has to provide an area for your plugin to output such as the content area or via a widget area. 对网站主题的访问受限。 插件无法将代码直接输出到主题的某个位置。 主题必须提供一个供插件输出的区域,例如内容区域或通过小部件区域。
- Plugin conflicts do occur. Sometimes another plugin (or even the theme) will negatively affect your plugin and it can ruin the experience (for example, you could be using the WordPress media uploader and it has been altered and not longer functions as expected, ruining your plugin). 确实发生插件冲突。 有时,另一个插件(甚至主题)会对您的插件产生负面影响,并且可能破坏体验(例如,您可能正在使用WordPress媒体上传器,并且该插件已被更改且不再能按预期运行,从而破坏了您的插件)。
- Limited access to templates and design. Plugins are supposed to provide the functionality and the theme provides the styling. 对模板和设计的访问受限。 插件应提供功能,而主题应提供样式。
核心概念–操作,过滤器,简码,小部件等 (Core Concepts – Actions, Filters, Shortcodes, Widgets and More)
Let’s take a moment to talk about some key aspects of plugin development.
让我们花一点时间讨论插件开发的一些关键方面。
You might be familiar with these areas if you have worked on WordPress themes, however, a solid understanding of how these concepts work will help you build easy to use and maintainable functionality.
如果您曾经处理过WordPress主题,那么您可能熟悉这些领域,但是,对这些概念的工作原理有深入的了解将有助于您构建易于使用和可维护的功能。
动作(挂钩) (Actions (Hooks))
An action
hook is an activity that is performed by WordPress at a specific time. When this action is triggered it will find all functions that have been attached to this action and execute them in the correct order.
action
挂钩是WordPress在特定时间执行的活动。 触发此操作后,它将找到该操作已附加的所有功能,并以正确的顺序执行它们。
WordPress has dozens of actions defined throughout its core functionality, each action consisting of a unique name. For example, when WordPress is saving a post (such as a post, page or other content type) it will call the action save_post
which will look for any functions attached to this action.
WordPress在其整个核心功能中定义了许多动作,每个动作都由一个唯一的名称组成。 例如,当WordPress保存帖子(例如帖子,页面或其他内容类型)时,它将调用操作save_post
,该操作将查找与此操作相关的所有功能。
All actions are defined using the do_action()
function. This function takes in the following parameters
所有动作均使用do_action()
函数定义。 该函数采用以下参数
$tag
(required)$tag
(必填)$args
(optional one or more variables)$args
(可选的一个或多个变量)
Overall, each action will have its name (its tag) and also an optional number of additional useful variables (there could be multiple or zero additional variables