php do_action 和 add_action 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26781857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:03:34  来源:igfitidea点击:

How do do_action and add_action work?

phpwordpress-pluginwordpress

提问by Vignesh Pichamani

I am trying to find what exactly do_action and add_action works. I already examine with add_action but for do_action i am trying as new now. This what i tried.

我正在尝试找出 do_action 和 add_action 的工作原理。我已经使用 add_action 进行了检查,但是对于 do_action 我现在正在尝试作为新的。这是我试过的。

function mainplugin_test() {

$regularprice = 50;

if(class_exists('rs_dynamic')) {
$regularprice = 100;
}

// and doing further
//like i echoing the regular price
echo $regularprice; //It print 100 from this code

}

Now instead of placing few code in main file i am planning to create do_action to avoid code messing issue.

现在,我打算创建 do_action 以避免代码混乱问题,而不是在主文件中放置少量代码。

    function mainplugin_test() {

    $regularprice = 50;

    do_action('testinghook');

// and doing further
//like i echoing the regular price
echo $regularprice; //It should print 100 but it print 50

    }

so i created another function to point out that hook as something like

所以我创建了另一个函数来指出这个钩子就像

function anothertest() {
if(class_exists('rs_dynamic')) {
$regularprice = 100;
}
}
add_action('testinghook','anothertest');

Not sure how to add the lines of code to that hook that above function may work? As per i tried in my testing environment nothing helps. If i understand correct do_action is more like including a file??? If not please advise me.

不确定如何将代码行添加到上述函数可能工作的钩子中?根据我在测试环境中的尝试,没有任何帮助。如果我理解正确的 do_action 更像是包含一个文件???如果没有,请告诉我。

Thanks.

谢谢。

采纳答案by Rahil Wazir

The reason it didn't print 100, because $regularpricewithin anothertest()function is local to that function. The variable $regularpriceused in parent mainplugin_test()function is not same as the variable used in anothertest()function, they are in separate scope.

它没有打印的原因100,因为$regularpriceanothertest()函数内是该函数的本地。变量$regularprice在父使用mainplugin_test()功能不一样的变量中使用的anothertest()功能,它们是在单独的范围。

So you need to either define the $regularpricein a global scope (which is not a good idea) or you can pass argument as a parameter to do_action_ref_array. The do_action_ref_arrayis the same as do_actioninstead it accepts second parameter as array of parameters.

因此,您需要$regularprice在全局范围内定义 the (这不是一个好主意),或者您可以将参数作为参数传递给do_action_ref_array. 这do_action_ref_arraydo_action它接受第二个参数作为参数数组相同。

Passing as argument:

作为参数传递:

function mainplugin_test() {

    $regularprice = 50;

    // passing as argument as reference
    do_action_ref_array('testinghook', array(&$regularprice));

    echo $regularprice; //It should print 100

}

// passing variable by reference
function anothertest(&$regularprice) {
    if(class_exists('rs_dynamic')) {
        $regularprice = 100;
    }
}
// remain same
add_action('testinghook','anothertest');

回答by diggy

do_actioncreates an action hook, add_actionexecutes hooked functions when that hook is called.

do_action创建一个动作钩子,add_action在调用该钩子时执行钩子函数。

For example, if you add the following in your theme's footer:

例如,如果您在主题的页脚中添加以下内容:

do_action( 'my_footer_hook' );

You can echo content at that location from functions.php or a custom plugin:

您可以从 functions.php 或自定义插件在该位置回显内容:

add_action( 'my_footer_hook', 'my_footer_echo' );
function my_footer_echo(){
    echo 'hello world';
}

You can also pass variables to a hook:

您还可以将变量传递给钩子:

do_action( 'my_footer_hook', home_url( '/' ) );

Which you can use in the callback function:

您可以在回调函数中使用它:

add_action( 'my_footer_hook', 'my_footer_echo', 10, 1 );
function my_footer_echo( $url ){
    echo "The home url is $url";
}

In your case, you're probably trying to filter the value based on a condition. That's what filter hooks are for:

在您的情况下,您可能正在尝试根据条件过滤值。这就是过滤器钩子的用途:

function mainplugin_test() {
    echo apply_filters( 'my_price_filter', 50 );
}

add_filter( 'my_price_filter', 'modify_price', 10, 1 );
function modify_price( $value ) {
    if( class_exists( 'rs_dynamic' ) )
        $value = 100;
    return $value;
}

Reference

参考

回答by The Alpha

Actually, the add_actionis an action hook which is used to invoke an action (a registered handler) on a certain point depending on the action and the do_actionis used to manually invoke that registered action. For example:

实际上,add_action是一个动作钩子,用于根据动作在某个点调用动作(注册处理程序),并do_action用于手动调用该注册动作。例如:

add_action('some_hook', 'handler_for_some_hook');

This handler will be invoked when you take or the script does the some_actionbut if you want you may invoke that action manually using the do_action. So, basically the do_actioninvokes the registered action hook when you call it. Check more on Codex.

当您执行或脚本执行该处理程序时将调用此处理程序,some_action但如果您愿意,您可以使用do_action. 所以,基本上do_action当你调用它时调用注册的动作钩子。在Codex上查看更多信息。

回答by Dhruv

//with do_action we create own tag(hook) use in wordpress Ex. Here I added 1 custom function to call this In add_action I added function name with any custom tag name With do_action(my custom tag name)

// 使用 do_action 我们创建自己的标签(钩子)在 wordpress 中使用。在这里,我添加了 1 个自定义函数来调用它在 add_action 中,我添加了带有任何自定义标签名称的函数名称 with do_action(my custom tag name)

function mywork()
{
    echo "display my name";
}

add_action('mytag','mywork');

do_action('mytag');

---add_action()---

---add_action()---

hook-specific hook that will affect by function()

将受函数()影响的特定于钩子的钩子

 add_action( hook, **function**() )

It will change functionality and behaviour of wordpress by Specific “hook” we can change it and functions method.

它将通过特定的“钩子”改变 wordpress 的功能和行为,我们可以改变它和函数方法。