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:

    type: 'POST',
    url: 'http://localhost:8888/mywebsite/wp-admin/admin-ajax.php',
    data: {
        action: 'my_action',
        theTotal: newTotal
}).success(function (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

  • 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 May 24 at 13:25

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='')
| improve this answer | |
  • Thank you. I am trying to send the tag in ajax but clearly I am not doing it correctly as I am still getting the same error. data: { action: 'my_action', content: newTotal, tag: 'score' }, – Iggy's Pop May 24 at 12:30
  • You can't send arguments to the wp_ajax_ callback function with AJAX. You will need a separate function just for the AJAX callback that calls my_action_callback() with the arguments. – Jacob Peattie May 24 at 12:45
  • ay, I am getting lost now.. – Iggy's Pop May 24 at 13:03
  • @Iggy'sPop Did you add ,10,2 to the add_action? – cjbj May 24 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.