If you want to insert content into the posts table, you should create a new custom post type first. Once you have a custom post type, you can follow through with what you’re doing above with a couple changes.
<?php
function insert_post() {
// Check to make sure your content exists.
if ( ! isset( $_POST['text-post'] ) || empty ( $_POST['text-post'] ) ) {
return false;
}
// Sanitizing user input is extremely important.
$post_content = sanitize_textarea_field( $_POST['text-post'] );
// Used as an identifying string for this post. Not required.
$hash = wp_hash( $post_content );
// Build the insert post array.
$post_arr = array(
'post_status' => 'publish',
'post_author' => 1, // Set to the ID of author you want to associate this with.
'post_type' => 'your_custom_post_type', // Change this to your custom post type slug.
'post_content' => $post_content,
// Not required. I like to store a hash of the content to make sure it's only posted once.
'meta_input' => array(
$hash => 'import_hash',
)
);
return wp_insert_post( $post_arr, true );
}
The only thing I didn’t cover here is where the $_POST
content is coming from. I assume you already have that covered. If not, I recommend looking at making an AJAX endpoint.