WordPress

błąd krytyczny – wyniki admin-post.php na białym ekranie po przesłaniu formularza

  • 12 października, 2017
  • 4 min read
błąd krytyczny – wyniki admin-post.php na białym ekranie po przesłaniu formularza


To dziwny błąd, ponieważ nie występował wcześniej, właściwie wszystko działało dobrze. Usunąłem cały kod, który dodałem od czasu ostatniego testowania i działał, zanim się zepsuł.

Mam niestandardowy post działający jako strona do przesłania formularza i przetwarzam formularz za pomocą funkcji na stronie functions.php w mojej wtyczce.

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: Formularz działa, a dane są wstawiane do bazy danych, jedynym problemem jest to, że utknął na pustej (biały ekran) stronie admin-post.php. Co tu robię źle?

Warto przeczytać!  Jak dodać dynamiczną datę praw autorskich w stopce WordPress

AKTUALIZACJA:

Odkryłem, że przekierowuje tylko wtedy, gdy przesyłam obraz?


Źródło