如何在save_post操作上提供帖子附件?

问题描述 投票:0回答:1

我有以下代码从前端插入帖子:

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行动的优先权,但仍无法提供附件。

wordpress
1个回答
0
投票

看来你可以挂钩到publish_post

也许这个问题可以指导你正确的方向:

https://wordpress.stackexchange.com/questions/192258/execute-action-after-post-is-saved-with-all-related-post-meta-records-data

© www.soinside.com 2019 - 2024. All rights reserved.