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