我有以下代码从前端插入帖子:
if(!function_exists('zabeelAddMedicalHistory')){
function zabeelAddMedicalHistory()
{
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'create-report-nonce' ) ) {
die( 'Security check' );
} else {
$postID = wp_insert_post(array(
'post_type' => 'report',
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_status' => 'publish',
'post_content' => $_POST['postContent']
));
}
if ($postID) {
add_post_meta($postID, '_report_patient_id', $_REQUEST['_report_patient_id'], true);
if ($_FILES['postFile']) {
if ($_FILES['postFile']['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$uploadedFile = media_handle_upload('postFile', $postID );
if ( !is_wp_error( $uploadedFile ) ) {
$filename = $uploadedFile['file'];
$attachment = array(
'post_mime_type' => $uploadedFile['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $uploadedFile['url']
);
$attachment_id = wp_insert_attachment( $attachment, $uploadedFile['url'] );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
}
}
}
wp_redirect( wp_get_referer() );
exit;
}
add_action('wp_ajax_zabeelAddMedicalHistory', 'zabeelAddMedicalHistory');
add_action('wp_ajax_nopriv_zabeelAddMedicalHistory', 'zabeelAddMedicalHistory');
}
我会在保存帖子后通过save_post
操作向用户发送电子邮件。但是,在保存帖子并save_post
操作发挥作用时,save_post
操作尚未提供附件。我也尝试推迟save_post
操作的优先级,但仍然无法提供附件。
答案 0 :(得分:0)
似乎你可以挂钩publish_post
。
也许这个问题可以指导你正确的方向: