What plug to use wp_insert_post?

Hello, I want to create a feedback form that will send the results by email and write in the right category.
Created in the root ajax/web_form.php. In the file posted
$source = array(
 'post_title' => 'the title of the post.', / / header material.
 'post_name' => 'zagolovok-posta', // - "slug", a synonym for path.
 'post_excerpt' => 'Quote post.', // - quote the announcement.
 'post_content' => '<p>post Content.</p>', / / contents/content.
 'post_status' => 'publish', / / status of the material published.
 'post_author' => 1, // - the author of the material: the user with id=1 (admin).
 'post_type' => 'post', / / type of the content record.
 'post_category' => array( 11), / / topics.
 'tags_input' => 'adding a post, WordPress', / / label/tags.
 'comment_status' => 'open' // - comment to allow.
);

# Insert record into the database:
echo wp_insert_post($source);

beginning to swear that no function wp_insert_post hooked up wp-includes/post.php., beginning to swear or user_id is odd.
The next question that you need to connect to be able to use the functions and methods of the wp files.
July 8th 19 at 11:56
2 answers
July 8th 19 at 11:58
Solution
It is necessary to connect only one file
require($_SERVER[‘DOCUMENT_ROOT’].’/wp-load.php’);
Thanks for the reply.
connected to wp-load
wp_insert_post($source); return 0 - Solon.Howell commented on July 8th 19 at 12:01
And should return the id of the created post(post not created) - Solon.Howell commented on July 8th 19 at 12:04
July 8th 19 at 12:00
Solution
It is not necessary to do so. For Ajax have VP there are special things done(simplistically) in functions.php:
add_action('wp_ajax_sendmail', 'sendmail_callback');
add_action('wp_ajax_nopriv_sendmail', 'sendmail_callback');
sendmail_callback function() {
 $data = $_POST; //here we have all the data sent by js script
 //send mail, create record etc.
}

In js(jquery):
var data = {
 action: 'sendmail',
 //mail, name, or other fields for example 
 mail: jQuery("#mail").val()
};
jQuery.post('/wp-admin/admin-ajax.php', data, function(response) {
//get the answer, do what you need or print the error
}, "json");


If returns 0, then try setting $wp_error to true, i.e. echo wp_insert_post($source, true); and see what error it returns.

Find more questions by tags WordPress