如何为Woocommerce的自定义插件添加自定义钩子 [英] How to add custom hooks to a custom plugin for Woocommerce

查看:478
本文介绍了如何为Woocommerce的自定义插件添加自定义钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经为Woocommerce支付集成开发了一个自定义插件,我们需要在我们的插件文件中添加woocommerce挂钩,而不要将其包含在Theme文件夹的function.php 中.

We have develop a custom plugin for Woocommerce payment integration and we need to add woocommerce hooks, in our plugin file without including it in Theme folder's function.php.

如何将自定义钩子添加到我们可以从function.php文件调用的插件中?有什么解决方法吗?
(当我们在wordpress主题的function.php文件中添加自定义woocommerce挂钩和操作代码,然后代码可以正常工作,但我们在自定义插件文件中需要它.)

How can we Add custom hooks to our plugin that we can invoke from function.php file? is there any workaround for it?
(when we add custom woocommerce hooks and action code in function.php file in wordpress theme then code work fine but we need it in our custom plugin file.)

任何帮助将不胜感激.

Any help will be highly appreciated.

推荐答案

有两种钩子:操​​作钩子和过滤器钩子.

动作挂钩就像是某些代码中的大门或门,允许您在特定的代码位置运行某些自定义代码.当处理该门或大门的代码运行时,它将被执行或触发". 所以它是基于事件的.

There is 2 kind of hooks: Action hooks and filter hooks.

An action hook is like a gate or a door in some code, that allow you to run some custom code, in a specific code location. It will be executed or "triggered", when the code that handle that door or gate runs. So it's event based.

过滤器挂钩与动作挂钩有些不同.它们不用于触发某些自定义代码(不是基于事件的).它们允许更改或操纵一些现有的代码值,例如字符串,数组,对象……因此,过滤器钩子总是在一个参数的最小值处进行操作,并且将始终返回该操纵的参数 >.

Filter hooks are a bit different than action hooks. They are not use to trigger some custom code (not event based). They allow to alter or manipulate some existing code values, as strings, arrays, objects… So filter hooks have always at minima one argument to be manipulated and will always return that manipulated argument.

1)用于动作挂钩: do_action() WordPress功能

1) For action hooks: do_action() Wordpress function

do_action('woocommerce_my_custom_action', $some_variable, $another_variable );

2)用于过滤器挂钩: apply_filters() Wordpress函数(其中$value是操纵参数)

2) For filter hooks: apply_filters() Wordpress function (where $value is the manipulated argument)

$value = apply_filters('woocommerce_my_custom_action', $value, $some_variable, $another_variable );

Internet上有很多有关钩子的文档,教程和资源

There is a bunch of documentation, tutorials and ressources on internet regarding hooks

WordPress过滤器与.动作

动作钩子与WordPress中的过滤器钩子?

WordPress编写自定义操作钩子

这篇关于如何为Woocommerce的自定义插件添加自定义钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆