• I have a WordPress function that works exactly how I want. The function below changes the email recipient in Contact Form 7 to [email protected]. Now I need to use ajax to update the email recipient to a dynamic value.

    function wpcf7_dynamic_email_field($args) {
    if(!empty($args[‘recipient’])) {
    $args[‘recipient’] = str_replace(‘%admin%’, ‘[email protected]’, $args[‘recipient’]);
    return $args;
    }
    return false;
    }
    add_filter(‘wpcf7_mail_components’, ‘wpcf7_dynamic_email_field’);
    Here’s my ajax call. I do not know how to tell the ajax call to initiate the wpcf7_dynamic_email_field() function. Can that be done?

    $.ajax({
    url: ajaxurl, // or example_ajax_obj.ajaxurl if using on frontend
    data: {
    ‘action’: ‘update_team_page_contact_form’,
    ’emailAddress’ : emailAddress
    },
    success:function(data) {
    // This outputs the result of the ajax request
    console.log(data);
    },
    error: function(errorThrown){
    console.log(errorThrown);
    }
    });

Viewing 4 replies - 1 through 4 (of 4 total)
  • No, this won’t work the way you intend.
    The AJAX call is separate from the call that produced the page and from the submit, so there is nothing to filter.

    If your input for the email address is on the same page as the contact form, you can modify the data sent by the form when it is submitted. You don’t need AJAX for that.
    If your input is somewhere else, you will need to save your value somewhere so that it is always available to filter into the field. Or you can save it in the plugins option using a filter on the option name.

    Thread Starter packerblitz

    (@packerblitz)

    Thanks Joy.

    “If your input for the email address is on the same page as the contact form, you can modify the data sent by the form when it is submitted. You don’t need AJAX for that.”

    What I’m doing is when somebody clicks on Al, I want to change the email recipient setting in Contact Form 7 to Al’s email address. When somebody clicks on Barb, change to Barb, etc. Do you know how I would change the data sent by the form when it is submitted? It would seem like I would need to do that with AJAX or JS.

    AJAX is typically written in JS. But AJAX talks to the server. If there’s no need to talk to the server, you don’t use AJAX. If the form is in the same page, you just put the selected email into the form field, so it will be used when the form is Submitted.

    For details of the plugin, you should ask at the plugin’s support forum. They know their code best. (There are 50,000 plugins, you know.)

    Moderator bcworkz

    (@bcworkz)

    What I would do to achieve your goal is to place a dropdown select field in the contact form which contains all possible email recipients. Use your filter callback to grab the selected field value from $_POST and use it to set the appropriate email recipient argument. Untested, but it should work. It’s essentially what one would do with a hand coded form that does not rely upon a plugin.

    Due to privacy concerns, don’t place actual email addresses in the dropdown, only place arbitrary values which your callback can use to determine the actual email address.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WordPress ajax call with add_filter’ is closed to new replies.