Hello. I would like to immediately say that the popular WP add-ons with forms do not suit me and therefore I am looking for a way to send my own forms from a modal window.

The scheme is approximately the following: On the usual WP pages (page.php), the Fancybox script is connected , which helps with Popups . They are triggered by pushing buttons with jQuery. The page itself has the code of the html-form in a hidden form, it appears only inside the modal window. Next you need to send this form to the post office, but with this it is still difficult.

JS on the form:

$(document).ready(function() { function validatePhone(phone7) { var reg = /[0-9,\s]/i; return reg.test(phone7); } $("#calculation_form").submit(function() { return false; }); var pageurl = window.location.href; $("#url2").val(pageurl); $("#calculation_submit").on("click", function(){ var phoneval = $("#phone7").val(); var phonevalid = validatePhone(phoneval); if(phonevalid == false) { $("#phone7").addClass("error"); } else if(phonevalid == true){ $("#phone7").removeClass("error"); } $('#calculation_form .custom_checkbox').each(function() { if($(this).prop('checked')) { $(this).val('Да'); } }); if(phonevalid == true) { $.ajax({ type: 'POST', url: '/script/phpmailer.php', data: $("#calculation_form").serialize(), success: function(data) { if(data == "true") { $("#calculation_popup").fadeOut("fast", function(){ $(this).before("<div class='success_msg'>Ваша заявка успешно отправлена!<br>С Вами свяжутся в ближайшее время.</div>"); setTimeout("$.fancybox.close()", 3000); }); $('#calculation_form').each(function(){ this.reset(); }); } } }); } }); }); 

Actually the question is: how to link action = "" from a form from a regular WP page with phpmailer to send a letter? Surely there are pitfalls and you need to dig into functions.php or take into account the feature of CMS.

On many resources ( for example ) they write something similar can be done with admin-post.php , but as I understand it, you need to write the entire form along with the html in the PHP file and on the WP page, in a modal window, somehow call this .php. Also, there are many English-language articles about the contact form without plug-ins ( for example ), but this is still not the case and I do not need one specific page. This form window should be used on many pages.

I will be glad to any useful information. I do not need a fully prepared practical part (although it would not be bad), at least theoretical actions to figure out how it works.

    1 answer 1

    Actually in your reasoning there is some truth. You can do as you say

    1. You can create a page with an alias (for example "system-sendmail")
    2. Create a file in your current theme page-system-sendmail.php
    3. In the url ajax request query '/script/phpmailer.php' change to '/ system-sendmail /' That's all

    It remains for you:

    1. In page-system-sendmail.php connect phpmailer
    2. Check POST input parameters
    3. Add ajax check
    4. To send a letter

    PS Everything can be done in a more native way through the WP engine. see the wp_ajax_my_action hook (and by analogy you can find hooks, for other conditions).

    This answer conveys my personal vision of solving the problem.

    • From this point in more detail “standard WP ajax handler (it often hangs)” - KAGG Design
    • @KAGGDesign 1. where it’s clearer to see in the admin panel what ajax handlers are, than to search and watch them in func..php 2. put a plugin to watch your handlers redundant occupation 3. when you create a blank page with your logic, you get almost the same thing as in the formation of ajax through the hook (wp_ajax_my_action). But with such logic, you leave most of the logic in func..php, which is good, because everything can be stored in the same .js file - lazyproger
    • @KAGGDesign with large clutter in the database, pages are given faster than ajax request. - lazyproger
    • I asked you about the hang. What makes you think that the standard ajax handler often hangs. - KAGG Design
    • @KAGGDesign "when the database is cluttered up, the pages are delivered faster than the ajax request" on medium and small volumes of database, there is no difference, just where the logic is stored. On large projects, hangs sometimes happen (crashing xhr, where the root of evil I have no idea, didn’t dig WP so deeply). Corrected the answer (all the same, not often, and sometimes) - lazyproger