Passing arguments with add_action and do_action th

2019-08-29 12:34发布

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.

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

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);

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);
};

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?

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)

标签: wordpress
1条回答
SAY GOODBYE
2楼-- · 2019-08-29 13:20

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
}
查看更多
登录 后发表回答