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:
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 1Uncaught ArgumentCountError: Too few arguments to function my_action_callback(), 1 passed in and exactly 2 expected
- 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
1 Answer
Reset to default 0The 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
版权声明:本文标题:filters - post value to function with Ajax and jQuery 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742405644a2468764.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论