Why Wordpress action callback does not fire when added inside function

三世轮回 提交于 2020-06-29 04:28:26

问题


I'm wondering why

add_action( 'admin_post_nopriv_myAction', 'myCallback' );

does not fire when I add it inside a function, but it does work when added outside a function. See examples below. I minimized the code a bit to show the idea. In reality a form is output in the shortcode function and processed in the callback.

This one does not fire the callback:

function my_shortcode() {
    add_action( 'admin_post_nopriv_myAction', 'myCallback' );
    add_action( 'admin_post_myAction', 'myCallback' );

    return '<p>my html form code</p>';
} 
add_shortcode('MYSHORTCODE', 'my_shortcode');

function myCallback () {
    // Process a form
}

While this does fire the callback:

function my_shortcode() {
    return '<p>my html form code</p>';
} 
add_shortcode('MYSHORTCODE', 'my_shortcode');

function myCallback () {
    // Process a form
}
add_action( 'admin_post_nopriv_myAction', 'myCallback' );
add_action( 'admin_post_myAction', 'myCallback' );

In the second example I do get to the white mydomain/wp-admin/admin-post.php screen. But the callback is not called and so my form is not processed.

来源:https://stackoverflow.com/questions/62212051/why-wordpress-action-callback-does-not-fire-when-added-inside-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!