尝试在前端创建一个带有AJAX调用的表单,用户可以在其中提交帖子。
有一个文本字段、一个文本区域和一个文件字段。
以下是表格:
public function pp_html_template() {
if ( is_user_logged_in() ) {
return \'<h2>\' . __( \'Publish your post\', \'post-publisher\' ) . \'</h2>
<form id="pp-form-submit" class="pp-form-submit" enctype="multipart/form-data">\' .
wp_nonce_field( \'pp_publisher_save\', \'pp_publisher_name\' )
. \'<div class="pp-row">
<label for="pp_title">\' . esc_attr__( \'Title\', \'post-publisher\' ) . \'</label>
<input type="text" id="pp_title" name="pp_title">
</div>
<div class="pp-row">
<label for="pp_content">\' . esc_attr__( \'Content\', \'post-publisher\' ) . \'</label>
<textarea name="pp_content" id="pp_content" name="pp_content" cols="30" rows="10"></textarea>
</div>
<div class="pp-row">
<label for="pp_featured_image">\' . esc_attr__( \'Featured Image\', \'post-publisher\' ) . \'</label>
<input type="file" id="pp_featured_image" name="pp_featured_image">
</div>
<input type="hidden" name="action" value="pp_html_process"/>
<div class="pp-row">
<input type="submit" name="pp_submit" id="pp_submit">
</div>
</form>\';
}
}
以下是处理过程:public function pp_html_process() {
// Process the form
if ( isset( $_POST[\'pp_submit\'] ) ) {
if ( ! isset( $_POST[\'pp_publisher_name\'] ) || ! wp_verify_nonce( $_POST[\'pp_publisher_name\'], \'pp_publisher_save\' ) ) {
esc_attr__( \'Sorry, this action is not allowed.\', \'post-publisher\' );
exit;
} else {
global $current_user;
$user_login = $current_user->user_login;
$user_id = $current_user->ID;
$post_title = sanitize_text_field( $_POST[\'pp_title\'] );
$post_content = sanitize_textarea_field( $_POST[\'pp_content\'] );
$new_post = array(
\'post_title\' => $post_title,
\'post_content\' => $post_content,
\'post_type\' => \'post\',
\'post_status\' => \'draft\',
\'post_name\' => str_replace( \' \', \'-\', $post_title ),
);
$post_id = wp_insert_post( $new_post, true );
if ( ! function_exists( \'wp_generate_attachment_metadata\' ) ) {
require_once( ABSPATH . "wp-admin" . \'/includes/image.php\' );
require_once( ABSPATH . "wp-admin" . \'/includes/file.php\' );
require_once( ABSPATH . "wp-admin" . \'/includes/media.php\' );
}
$featured_image = media_handle_upload( \'pp_featured_image\', $post_id );
if ( $featured_image > 0 ) {
update_post_meta( $post_id, \'_thumbnail_id\', $featured_image );
}
}
}
}
以及__construct()
public function __construct() {
if ( ! is_admin() ) {
add_shortcode( \'pp_html_template\', array( $this, \'pp_html_template\' ) );
add_action( \'init\', array( $this, \'pp_html_process\' ) );
}
add_action( \'wp_ajax_pp_html_process\', array( $this, \'pp_html_process\' ) );
add_action( \'wp_ajax_nopriv_pp_html_process\', array( $this, \'pp_html_process\' ) );
}
jQuery:jQuery(document).ready(function ($) {
$(\'#pp-form-submit\').submit(ajaxSubmit);
function ajaxSubmit() {
var newCustomerForm = $(this).serialize();
jQuery.ajax({
type: "POST",
url: "/codeable/wp-admin/admin-ajax.php",
data: $("#pp-form-submit").serialize(),
success: function (response) {
console.log(response);
}
});
return false;
}
});
控制台中没有任何错误。没有AJAX的Post工作得非常好,但有了AJAX,它将返回0。任何建议都会很有帮助。