Php 为什么不是';t Wordpress&x27';保存帖子{$post->;帖子类型}';动作钩子传递正确数量的参数?
根据WordPress codex,应传递三个参数:Php 为什么不是';t Wordpress&x27';保存帖子{$post->;帖子类型}';动作钩子传递正确数量的参数?,php,wordpress,Php,Wordpress,根据WordPress codex,应传递三个参数: do\u操作(“保存帖子”{$post->post\u type}”、int$post\u ID、WP\u post$post、bool$update) 这是我的代码: add_action( 'save_post_guide', array($this, 'saveGuideMeta') ); function saveGuideMeta(int $post_ID, WP_Post $post, bool $update) { if
do\u操作(“保存帖子”{$post->post\u type}”、int$post\u ID、WP\u post$post、bool$update)
这是我的代码:
add_action( 'save_post_guide', array($this, 'saveGuideMeta') );
function saveGuideMeta(int $post_ID, WP_Post $post, bool $update) {
if (isset($_POST['guide_keyword'])) {
update_post_meta($post_ID, 'guide_keyword', sanitize_text_field($_POST['guide_keyword']));
}
}
我确实复制了函数签名,但当我保存帖子时,它抛出了一个ArgumentCountError
(因此我知道函数正在被调用,钩子正在“工作”)
例外情况
致命错误:Uncaught ArgumentCounter错误:函数saveGuideMeta()的参数太少,在第288行的public_html/wp includes/class-wp-hook.php中传递了1个参数,预期正好有3个参数
这就好像save\u post\u指南
hook没有传递三个参数,只有一个
我只是试图在保存帖子时更新帖子元。我做错了什么 如果代码位于
functions.php
中,则可能需要将操作代码编写为:
add_action( 'save_post_guide', 'saveGuideMeta' );
如果代码在plugin类中,并且您正在类的构造函数中调用action,那么您需要将saveGuideMeta函数设置为public,如下所示
public function saveGuideMeta(int $post_ID, WP_Post $post, bool $update) {
if (isset($_POST['guide_keyword'])) {
update_post_meta($post_ID, 'guide_keyword', sanitize_text_field($_POST['guide_keyword']));
}
}
传递默认的$accepted_args
;该值为1
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
这需要设置为3才能获得全部3。感谢您的快速回复。实际上我刚刚发现了问题所在。add#action还有两个参数:priority和#个已传递的已接受参数。因此,我不得不将我的add_操作更改为
add_操作('save_post_guide',array($this,'saveGuideMeta'),10,3)代码>--3告诉它传递三个参数。你知道的越多……;)