我试图通过在wordpress上创建一个相对简单的应用程序来学习PHP,这是一个基本的CRM,我已经取得了不错的进步,但在一些细节上遇到了困难。
在用户创建新客户后,让wp\\u redirect()工作时,我遇到了一些严重的“headers ready sent”问题。
下面是我想做的一个例子:
新客户。php:通过前端创建一个新的自定义帖子类型,然后重定向到特定客户的单个帖子页面。代码如下:
<?php
$address_line_1 = $_POST[\'address_line_1\'];
$address_line_2 = $_POST[\'address_line_2\'];
$suburb = $_POST[\'suburb\'];
$state = $_POST[\'state\'];
$post_code = $_POST[\'post_code\'];
$country = $_POST[\'country\'];
$email = $_POST[\'email\'];
$first_name = $_POST[\'first_name\'];
$last_name = $_POST[\'last_name\'];
$phone_number = $_POST[\'phone_number\'];
$progress_identifier = \'1\';
// PROCESS THE FORM
if (\'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty($_POST[\'action\']) && $_POST[\'action\'] == "new_customer")
{
$new_customer = array(
\'post_title\' => wp_rand(100,999).\'-\'.wp_rand(),
\'post_author\' => $user_ID,
\'post_status\' => \'publish\',
\'post_type\' => \'customer\'
);
$pid = wp_insert_post($new_customer);
add_post_meta($pid, \'address_line_1\', $address_line_1, true);
add_post_meta($pid, \'address_line_2\', $address_line_2, true);
add_post_meta($pid, \'suburb\', $suburb, true);
add_post_meta($pid, \'state\', $state, true);
add_post_meta($pid, \'post_code\', $post_code, true);
add_post_meta($pid, \'country\', $country, true);
add_post_meta($pid, \'email\', $email, true);
add_post_meta($pid, \'first_name\', $first_name, true);
add_post_meta($pid, \'last_name\', $last_name, true);
add_post_meta($pid, \'phone_number\', $phone_number, true);
add_post_meta($pid, \'address\', $address, true);
add_post_meta($pid, \'progress_identifier\', $progress_identifier, true);
$survey_post = array(
\'post_title\' => $pid . \'-\' . wp_rand(),
\'post_author\' => $user_ID,
\'post_status\' => \'publish\',
\'post_type\' => \'survey\'
);
$survey_id = wp_insert_post($survey_post);
global $wpdb;
$wpdb->insert(
$wpdb->p2p,
array(
\'p2p_from\' => $survey_id,
\'p2p_to\' => $pid,
\'p2p_type\' => \'survey_to_customer\'
)
);
wp_redirect(home_url());
exit();
}
?>
此代码放在HTML表单之后。
我知道在页面开始输出任何内容之前必须调用wp\\u redirect(),但我不确定如何实现这一点,因为我已经尝试将其放在页面的更高位置,但没有任何效果。
请随意滥用我糟糕的代码和模棱两可的问题,但如果你能为我指出正确的方向,以实现我正在努力实现的目标,那将是伟大的!我曾尝试将事情分解成函数,通过“save\\u post”挂钩进行调用,但也没有成功。
感谢阅读!