admin-post.php results on white screen after form submission

Question

This is a strange error because it wasn’t happening earlier, it was actually all working fine. I have removed all the code I have added since I last tested it and it was working before it broke.

I have a custom post acting as a page to submit a form on and I am processing the form via a function in the functions.php page in my plugin.

function tm_add_new_job() {

    if ( empty($_POST) || !wp_verify_nonce($_POST['tm_add_new_job'],'tm_add_new_job') ) {

        wp_nonce_ays();

        die();

    } else {

        $type = $_POST['contacttype'];

        switch ($type) {

            case 'email':

                $title   = $_POST['title'];
                $name    = $_POST['name'];
                $surname = $_POST['surname'];
                $email   = $_POST['email'];

                if (!empty($email)) {

                    // Post new job and customer
                    $new_customer_id = tm_add_customer($title, $name, $surname);

                    $new_job_id      = tm_add_job($new_customer_id);

                    // Assign link to about_you page
                    $about_you       = get_page_by_path( 'about-you', '', 'jobs' );

                    $linked          = get_post_permalink($about_you->ID);

                    $secret          = get_post_meta( $new_customer_id, 'customer_secret', true );

                    $link            = $linked .'?customer='. $new_customer_id .'&secret='. $secret;

                    tm_mail_lead($email, $from, $custom, $link);

                    // Add job ID to customer
                    add_metadata( 'post', $new_customer_id, 'job_id', $new_job_id, false );

                    if (!empty($title)) {

                        // Add customer title
                        add_metadata( 'post', $new_customer_id, 'customer_title', $title, false );

                    }

                    if (!empty($name)) {

                        // Add customer name
                        add_metadata( 'post', $new_customer_id, 'customer_name', $name, false );

                    }

                    if (!empty($surname)) {

                        // Add customer surname
                        add_metadata( 'post', $new_customer_id, 'customer_surname', $surname, false );

                    }

                    // Add customer email
                    add_post_meta( $new_customer_id, 'customer_email', $email, false );

                } else {

                    // No email - Fail
                    wp_redirect( tm_new_job_link());
                }

                break;

            case 'text':

                $title   = $_POST['sms_title'];
                $name    = $_POST['sms_name'];
                $surname = $_POST['sms_surname'];
                $mobile  = $_POST['mobile'];

                if (!empty($mobile)) {

                    // Post new job and customer
                    $new_customer_id = tm_add_customer($title, $name, $surname);

                    $new_job_id      = tm_add_job($new_customer_id);

                    // Add job ID to customer
                    add_metadata( 'post', $new_customer_id, 'job_id', $new_job_id, false );

                    if (!empty($title)) {

                        // Add customer title
                        add_metadata( 'post', $new_customer_id, 'customer_title', $title, false );

                    }

                    if (!empty($name)) {

                        // Add customer name
                        add_metadata( 'post', $new_customer_id, 'customer_name', $name, false );

                    }

                    if (!empty($surname)) {

                        // Add customer surname
                        add_metadata( 'post', $new_customer_id, 'customer_surname', $surname, false );

                    }

                    // Add customer email
                    add_metadata( 'post', $new_customer_id, 'customer_mobile', $mobile, false );

                } else {

                    // No Mobile Number - Fail
                    wp_redirect( tm_new_job_link());
                }


                break;

            case 'customer':

                $image = 'quoting.png';

                break;

        }

        if ($_FILES) {

            // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');

            $file = $_FILES[featured_image];

            $file_return = wp_handle_upload( $file, array('action' => 'tm_add_new_job' ) );

            if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {

              return false;

            } else {

                $filename = $file_return['file'];

                $attachment = array(
                    'post_mime_type' => $file_return['type'],
                    'post_title'     => preg_replace( '/.[^.]+$/', '', basename( $filename ) ),
                    'post_content'   => '',
                    'post_status'    => 'inherit',
                    'guid' => $file_return['url']
                );

                $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );

                // Generate the metadata for the attachment, and update the database record.
                $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
                wp_update_attachment_metadata( $attachment_id, $attachment_data );

                set_post_thumbnail( $new_job_id, $attachment_id );

            }

        }

        // success go to new job post.
        wp_redirect( get_permalink($new_job_id));
    }

}
add_action( 'admin_post_tm_add_new_job', 'tm_add_new_job' );

FYI: The form works and the data is inserted into the database, the only problem is it is stuck on a blank (white screen) admin-post.php page. What am I doing wrong here?

UPDATE:

I have discovered that it redirects only if I upload an image?

0
, lukgoh 3 years 2020-06-04T08:10:39-05:00 0 Answers 90 views 0

Leave an answer

Browse
Browse