试图理解 WordPress 插件样板
Posted
技术标签:
【中文标题】试图理解 WordPress 插件样板【英文标题】:Trying to understand WordPress Plugin Boilerplate 【发布时间】:2019-11-26 01:39:18 【问题描述】:我是 php 和 WordPress 插件开发的新手。在WordPress Plugin Boilerplate 文件includes/class-plugin-name-loader.php 中,它声明了自己的add_action 函数。这是否覆盖了默认的 WordPress add_action 函数?如果是这样,我如何调用默认的 WordPress add_action 函数,如add_action( 'save_post', 'wpdocs_my_save_post', 10, 3 )
<?php
/**
* Register all actions and filters for the plugin
*
* @link http://example.com
* @since 1.0.0
*
* @package Plugin_Name
* @subpackage Plugin_Name/includes
*/
/**
* Register all actions and filters for the plugin.
*
* Maintain a list of all hooks that are registered throughout
* the plugin, and register them with the WordPress API. Call the
* run function to execute the list of actions and filters.
*
* @package Plugin_Name
* @subpackage Plugin_Name/includes
* @author Your Name <email@example.com>
*/
class Plugin_Name_Loader
/**
* The array of actions registered with WordPress.
*
* @since 1.0.0
* @access protected
* @var array $actions The actions registered with WordPress to fire when the plugin loads.
*/
protected $actions;
/**
* The array of filters registered with WordPress.
*
* @since 1.0.0
* @access protected
* @var array $filters The filters registered with WordPress to fire when the plugin loads.
*/
protected $filters;
/**
* Initialize the collections used to maintain the actions and filters.
*
* @since 1.0.0
*/
public function __construct()
$this->actions = array();
$this->filters = array();
/**
* Add a new action to the collection to be registered with WordPress.
*
* @since 1.0.0
* @param string $hook The name of the WordPress action that is being registered.
* @param object $component A reference to the instance of the object on which the action is defined.
* @param string $callback The name of the function definition on the $component.
* @param int $priority Optional. The priority at which the function should be fired. Default is 10.
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
*/
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 )
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
/**
* Add a new filter to the collection to be registered with WordPress.
*
* @since 1.0.0
* @param string $hook The name of the WordPress filter that is being registered.
* @param object $component A reference to the instance of the object on which the filter is defined.
* @param string $callback The name of the function definition on the $component.
* @param int $priority Optional. The priority at which the function should be fired. Default is 10.
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1
*/
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 )
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
/**
* A utility function that is used to register the actions and hooks into a single
* collection.
*
* @since 1.0.0
* @access private
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
* @param string $hook The name of the WordPress filter that is being registered.
* @param object $component A reference to the instance of the object on which the filter is defined.
* @param string $callback The name of the function definition on the $component.
* @param int $priority The priority at which the function should be fired.
* @param int $accepted_args The number of arguments that should be passed to the $callback.
* @return array The collection of actions and filters registered with WordPress.
*/
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args )
$hooks[] = array(
'hook' => $hook,
'component' => $component,
'callback' => $callback,
'priority' => $priority,
'accepted_args' => $accepted_args
);
return $hooks;
/**
* Register the filters and actions with WordPress.
*
* @since 1.0.0
*/
public function run()
foreach ( $this->filters as $hook )
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
foreach ( $this->actions as $hook )
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
【问题讨论】:
我刚刚注意到您是 *** 的新手。欢迎来到社区!我还想提醒您,如果他们回答您的问题或适当地帮助您找到正确的解决方案,请将答案标记为已接受。我在您的问题简介中注意到,您有一些未标记为已接受的已回答问题。确保这样做,以便帮助您的人获得他们应得的荣誉。 :) 【参考方案1】:第一:在您了解如何以普通的 WordPress 方式制作 WordPress 插件之前,请勿使用此 WordPress 插件样板。您发现的样板对初学者来说很复杂。
Google 可获取有关如何编写 wordpress 插件的任何初学者文档。并阅读有关 Actions、Hooks 和 Filters 的 WordPress 官方文档。这些是 WordPress 工作方式的主要内容。
这里有一个非常漂亮的概述:当您希望 WordPress 运行您的插件时,您可以为您的插件创建一个入口点函数。然后你调用 add_action() 向 WordPress 注册你的函数。然后 WordPress 将运行您的功能。只需在谷歌上搜索一些初学者文档以了解如何制作插件,它应该会向您展示所有这些示例。
其次: 看起来插件样板只是为您提供了一种模式,用于定义一组动作,这些动作将通过 run() 函数中的 foreach 循环添加到 wordpress 动作中。
这里没有压倒一切。 WordPress 使用函数式编程范式。与 WordPress 交互时,您通常不会使用对象。相反,WordPress 只是提供了一堆全局可用的函数,您可以调用这些函数来使用它。其中两个函数是 add_filter() 和 add_action()。
资源: 这是我找到的一个链接,其中包含大量用于创建 wordpress 插件的教程和参考资料: https://www.wpbeginner.com/wp-tutorials/how-to-create-a-wordpress-plugin/
【讨论】:
以上是关于试图理解 WordPress 插件样板的主要内容,如果未能解决你的问题,请参考以下文章