提交表单后,admin-post.php结果显示在白屏上

时间:2017-10-12 作者:lukgoh

这是一个奇怪的错误,因为之前没有发生,实际上一切正常。我已经删除了自上次测试以来添加的所有代码,并且在它崩溃之前它还在工作。

我有一个自定义帖子作为提交表单的页面,我正在通过函数中的函数处理表单。我的插件中的php页面。

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\' );
仅供参考:表单工作正常,数据被插入数据库,唯一的问题是它被粘贴在空白(白屏)管理帖子上。php页面。我做错了什么?

UPDATE:

我发现只有上传图像时才会重定向?

2 个回复
SO网友:lukgoh

问题是:

if ($_FILES) {}
有人能给我解释一下吗?wp\\u redirect()在if语句之外,为什么它不重定向?是不是因为if语句仍在启动,但由于并没有上载任何文件而出错?

SO网友:louisnelza

这可能是一个问题:

wp\\u重定向(tm\\u new\\u job\\u link());之后必须退出;

结束