使用 add_action 和 do_action 传递参数抛出错误“第一个参数应为有效回调" [英] Passing arguments with add_action and do_action throwing error 'First argument is expected to be a valid callback'
问题描述
我的 WordPress 页面上有一个表单,可在表单提交时将提交数据发送到第三方服务.该表单使用 Gravity Forms post-submit API 钩子 (http://www.gravityhelp.com/documentation/page/Gform_after_submission),虽然这不是错误的原因.
I have a form on my WordPress page that sends submission data to a third party service upon form submit. The form uses the Gravity Forms post-submit API hook (http://www.gravityhelp.com/documentation/page/Gform_after_submission), though thats not the cause for the error.
由于我的 JavaScript 参数传递有问题,我在加载我的网站时出现以下错误:
I have the following error appearing upon loading my site due to my JavaScript argument-passing having a problem:
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'aoExtPost' was given in /home2/jcollins/public_html/wp-includes/plugin.php on line 429
我的 WordPress 主题 functions.php 文件中有以下内容:
I have the following in my WordPress theme functions.php file:
if (function_exists('load_aoExtPost')) {
function load_aoExtPost() {
if (!is_admin()) {
wp_register_script('ExtPost', get_template_directory_uri() . '/teravoxel/js/ExtPost.js', array(), '0.1', true );
wp_enqueue_script('ExtPost');
}
}
}
//extPostUrl is the argument to pass
$extPostUrl = 'http://www.---webservice---/eform/3122/0027/d-ext-0002';
add_action('gform_after_submission_1', 'ExtPost', 10, 2);
do_action('gform_after_submission_1', $extPostUrl, $entry);
这是被引用的 JavaScript 的内容:
This is the content of the JavaScript being referenced:
function aoExtPost(extPostUrl) {
//generate iframe via some echoed out javascript
var aoUrl = extPostUrl;
var aoUrlStr = aoUrlA.toString();
var aoIfrm = document.createElement('iframe');
aoIfrm.setAttribute('id', 'ifrm');
aoIfrm.style.display='none';
aoIfrm.style.width='0px';
aoIfrm.style.height='0px';
aoIfrm.src = aoUrlStr;
document.body.appendChild(aoIfrm);
};
如果我用一个简单的 PHP 函数替换上面的 JS 文件引用只是为了测试 functions.php 中的参数传递,它就可以工作.有人能告诉我在移交给脚本的过程中我哪里出错了吗?
If I replace the above JS file reference with a simple PHP function just to test the argument-pass in functions.php, it works. Can someone tell me where I'm going wrong in the handoff to the script please?
目的是获取 $extPostUrl 的内容并将它们移动到 JS 生成的 iframe 源的查询字符串中(这就是我将数据传递给第三方服务的方式)
The purpose is to take the contents of $extPostUrl and move them into the JS-generated iframe's source's query-string (which is how I'm passing data to this third-party service)
推荐答案
正如警告所示,add_action
需要一个有效的回调函数.如果向钩子传递两个参数:
As the warning indicates, add_action
expects a valid callback function. If you pass two arguments to the hook:
do_action( 'gform_after_submission_1', $extPostUrl, $entry );
您可以在回调函数中对它们(其中一个或两个)进行处理:
You can do stuff with (one or both of) them in a callback function:
add_action( 'gform_after_submission_1', 'so20231440_extpost', 10, 2 );
function so20231440_extpost( $extPostUrl, $entry )
{
// do stuff with $extPostUrl and/or $entry
// for example:
wp_enqueue_script( 'extpost' );
wp_localize_script( 'extpost', 'extpost', array( 'url' => $extPostUrl ) );
// var usage in JS: extpost.url
}
这篇关于使用 add_action 和 do_action 传递参数抛出错误“第一个参数应为有效回调"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!