我创建了一个代码,将数据从Form保存到custom_post_type。 在Firefox上一切正常,但是当我在边缘对其进行测试时,该帖子仍会保存,但是我无法在admin上编辑该帖子,也无法查看该帖子(未找到对象)(参见图片)
我真的不知道问题出在哪里,请帮忙。
这是我的代码,我从我的function.php复制它:
function wpshout_frontend_post() {
wpshout_save_post_if_submitted();
?>
<div>
<div>
RESERVATION
</div>
<form id="new_post" class = 'datcho' name="new_post" method="post">
<input type = 'text' name = 'ten' required>
<input type = 'tel' name = 'sdt' required>
<input type = 'number' name = 'num' min='1' max = '10' required>
<input type = 'date' name = 'ngay' required>
<input type = 'time' name = 'time' value = '13:00' min='9:00' max='21:00' required>
<?php wp_nonce_field( 'wps-frontend-post' ); ?>
<input type="submit" value="Reservation" id="submit" name="submit"/>
</div>
</form>
</div>
<?php
}
function wpshout_save_post_if_submitted() {
// Stop running function if form wasn't submitted
if ( !isset($_POST['ten']) ) {
return;
}
// Check that the nonce was set and valid
if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') ) {
echo 'Did not save because your form seemed to be invalid. Sorry';
return;
}
// Do some minor form validation to make sure there is content
// Add the content of the form to $post as an array
$title = wp_strip_all_tags($_POST['ten']);
$post = array(
'post_title' => $title,
'post_content' => $title,
'post_status' => 'Pending',
'post_type' => 'datcho'
);
$eror = wp_insert_post($post,true);
if($eror){
$sdt = wp_strip_all_tags($_POST['sdt']);
$ngay = wp_strip_all_tags($_POST['ngay']);
$time = wp_strip_all_tags($_POST['time']);
$num = wp_strip_all_tags($_POST['num']);
add_post_meta($eror, 'sdt', $sdt);
add_post_meta($eror, 'ngay', $ngay);
add_post_meta($eror, 'time', $time);
add_post_meta($eror, 'num', $num);
echo 'Saved your post successfully! :)';
}else {
echo "something wrong";
}
}
答案 0 :(得分:0)
这是完整的解决方案。
问题主要是您没有在Edge中登录WP,因此在使用wp_insert_post
wordpress创建帖子时,不知道应该使用哪个帐户附加该帖子。
function wpshout_frontend_post() {
wpshout_save_post_if_submitted();
?>
<div>
<div>
RESERVATION
</div>
<form id="new_post" class = 'datcho' name="new_post" method="post">
<input type = 'text' name = 'ten' required>
<input type = 'tel' name = 'sdt' required>
<input type = 'number' name = 'num' min='1' max = '10' required>
<input type = 'date' name = 'ngay' required>
<input type = 'time' name = 'time' value = '13:00' min='9:00' max='21:00' required>
<?php wp_nonce_field( 'wps-frontend-post' ); ?>
<input type="submit" value="Reservation" id="submit" name="submit"/>
</form>
</div>
<?php
}
function wpshout_save_post_if_submitted() {
// Stop running function if form wasn't submitted
if ( !isset($_POST['ten']) ) {
return;
}
// Check that the nonce was set and valid
if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') ) {
echo 'Did not save because your form seemed to be invalid. Sorry';
return;
}
// Do some minor form validation to make sure there is content
// Add the content of the form to $post as an array
$title = wp_strip_all_tags($_POST['ten']);
$author_id = 1; // You can change it with your User ID
$post = array(
'post_title' => $title,
'post_content' => $title,
'post_status' => 'draft',
'post_type' => 'datcho'
'post_author' => $author_id
);
$eror = wp_insert_post($post,true);
if($eror){
$sdt = wp_strip_all_tags($_POST['sdt']);
$ngay = wp_strip_all_tags($_POST['ngay']);
$time = wp_strip_all_tags($_POST['time']);
$num = wp_strip_all_tags($_POST['num']);
add_post_meta($eror, 'sdt', $sdt);
add_post_meta($eror, 'ngay', $ngay);
add_post_meta($eror, 'time', $time);
add_post_meta($eror, 'num', $num);
echo 'Saved your post successfully! :)';
}else {
echo "something wrong";
}
}