admin管理员组

文章数量:1336631

This is how you would create a custom smart tag in wp forms:

function ea_custom_smart_tags( $content, $tag ) {
    if( 'my_smart_tag' == $tag ) {
        $value = 'Testing 1 2 3';
        $content = str_replace( '{' . $tag . '}', $value, $content );
    }
    return $content;
}
add_filter( 'wpforms_smart_tag_process', 'ea_custom_smart_tags', 10, 2 );

I want to post a value via ajax instead of hard coding it.

My jQuery/ajax:

jQuery.ajax({
    type: 'POST',
    url: 'http://localhost:8888/mywebsite/wp-admin/admin-ajax.php',
    data: {
        action: 'my_action',
        theTotal: newTotal
    },
}).success(function (result) {
    alert(result);
});

My functions.php:

function my_action_callback($content, $tag){


if ( 'total' === $tag ) {

 $total = $_POST['theTotal'];

 $content = str_replace( '{total}', $total, $content );

}

return $content;
}
add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_filter( 'wpforms_smart_tag_process', 'my_action_callback', 10, 2 );

This is however giving me an error:

Uncaught ArgumentCountError: Too few arguments to function my_action_callback(), 1 passed in and exactly 2 expected

This is how you would create a custom smart tag in wp forms:

function ea_custom_smart_tags( $content, $tag ) {
    if( 'my_smart_tag' == $tag ) {
        $value = 'Testing 1 2 3';
        $content = str_replace( '{' . $tag . '}', $value, $content );
    }
    return $content;
}
add_filter( 'wpforms_smart_tag_process', 'ea_custom_smart_tags', 10, 2 );

I want to post a value via ajax instead of hard coding it.

My jQuery/ajax:

jQuery.ajax({
    type: 'POST',
    url: 'http://localhost:8888/mywebsite/wp-admin/admin-ajax.php',
    data: {
        action: 'my_action',
        theTotal: newTotal
    },
}).success(function (result) {
    alert(result);
});

My functions.php:

function my_action_callback($content, $tag){


if ( 'total' === $tag ) {

 $total = $_POST['theTotal'];

 $content = str_replace( '{total}', $total, $content );

}

return $content;
}
add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_filter( 'wpforms_smart_tag_process', 'my_action_callback', 10, 2 );

This is however giving me an error:

Uncaught ArgumentCountError: Too few arguments to function my_action_callback(), 1 passed in and exactly 2 expected

Share Improve this question edited May 24, 2020 at 12:13 cjbj 15k16 gold badges42 silver badges89 bronze badges asked May 24, 2020 at 11:54 Iggy's PopIggy's Pop 1151 gold badge2 silver badges8 bronze badges 1
  • I see you used the old legacy Admin AJAX handler. Is there a reason you went for this rather than the simpler/modern REST API to handle your JS requests? – Tom J Nowell Commented May 24, 2020 at 13:25
Add a comment  | 

1 Answer 1

Reset to default 0

The error is quite self-explanatory. Your last lines are:

add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_filter( 'wpforms_smart_tag_process', 'my_action_callback', 10, 2 );

The action is calling the same function as the filter. But the filter passes two variables and the action only one. The function itself expects two variables:

function my_action_callback($content, $tag)

So if you call the function through the (ajax) action, you get this error. You'll have to pass $tag through your ajax call as well. Otherwise my_action_callback wouldn't do anything anyway, because it demands $tag to be 'total'. Or you could ditch $tag as a variable, since it is not doing a lot anyway.

Update: you can also make PHP be more forgiving by adding defaults in the function call:

my_action_callback($content='', $tag='')

本文标签: filterspost value to function with Ajax and jQuery