如果要在posts表中插入内容,应create a new custom post type 第一一旦你有了一个自定义的帖子类型,你就可以通过几个修改来完成上面所做的事情。
<?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 );
}
我唯一没有提到的是
$_POST
内容来自。我想你已经做过了。如果不是,我建议
making an AJAX endpoint.