S
S
Savely Dzvonkevich2016-11-05 14:59:25
MySQL
Savely Dzvonkevich, 2016-11-05 14:59:25

{Wordpress} After submitting the form, how do I create a post based on the filled in data?

Here is the form submission code. It is called by ajax.

<?php
    $to = '[email protected]'; адресов
    $subject = 'Заголовок'; 
    $message = '
            <html>
                <head>
                    <title>' . $subject . '</title>
                </head>
                <body>
                    <p>Имя: ' . $_POST['invoice-name'] . '<br />
                    Геолокация: ' . $_POST['invoice-city'] . '<br />
                    Программа интересует: ' . $_POST['invoice-msg'] . '<br />
                    Телефон: ' . $_POST['invoice-tel'] . '<br />
                    Эл. почта: ' . $_POST['invoice-email'] . '</p>                        
                </body>
            </html>';
    $headers  = "Content-type: text/html; charset=utf-8 \r\n"; 
    $headers .= "From: JoyFans <[email protected]> \r\n"; 
    mail($to, $subject, $message, $headers);

    return 200;
?>

Essence of the question:
I have an arbitrary type of posts, for example customers, arbitrary fields are tied to it through ACF.
I need that after submitting the form, a new record is created and the form data is filled in arbitrary fields. HELP

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-11-05
@Dzvonkevich

First, why are you writing a completely custom send when WordPress has wp_mail()?
The scheme is as follows:
1. You must send the form to admin-ajax.php using Ajax (google ajax in WordPress)
2. You must have your own callback / handler on the wp_ajax_{action} and wp_ajax_nopriv_{action} hooks
3. Validate the data in the handler ( you can't just disable $_POST['var'] without validation)
4. After validation, send a letter using wp_mail()
5. And create a new post using wp_insert_post()
6. After creating a post, you have its ID, add the necessary metadata via API ACF
7. Return what you need to the browser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question