问题
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