我找到了问题并解决了它。我编辑了以下内容:
add_complete_tour($tourTitle,$keywords,$sourceTour,$destinationTour,$duration,$tourContent,$agancyservice,$passports,$transport,$hotel_name,$hotelRate,$room1,$room2,$room3,$room4,$desc);
并添加
$reg_errors
到中的全局变量
add_complete_tour function
:
global $reg_errors,$tourTitle,$keywords,$sourceTour,$destinationTour,$duration,$tourContent,$agancyservice,$passports,$transport,$hotel_name,$hotelRate,$room1,$room2,$room3,$room4,$desc;
完整代码:
<?php
$postTitleError = \'\';
if(isset($_POST[\'submitted\']) && isset($_POST[\'post_nonce_field\']) && wp_verify_nonce($_POST[\'post_nonce_field\'], \'post_nonce\')) {
validate_tour_data(
$_POST[\'tourTitle\'],$_POST[\'keywords\'],$_POST[\'sourceTour\'],$_POST[\'destinationTour\'],$_POST[\'duration\'],$_POST[\'tourContent\'],$_POST[\'agancyservice\'],$_POST[\'passports\'],$_POST[\'transport\'],$_POST[\'hotel_name\'],$_POST[\'hotelRate\'],$_POST[\'room1\'],$_POST[\'room2\'],$_POST[\'room3\'],$_POST[\'room4\'],$_POST[\'desc\']
);
global $tourTitle,$keywords,$sourceTour,$destinationTour,$duration,$tourContent,$agancyservice,$passports,$transport,$hotel_name,$hotelRate,$room1,$room2,$room3,$room4,$desc;
$tourTitle = sanitize_text_field($_POST[\'tourTitle\']);
$keywords = sanitize_text_field($_POST[\'keywords\']);
$sourceTour = sanitize_text_field($_POST[\'sourceTour\']);
$destinationTour = sanitize_text_field($_POST[\'destinationTour\']);
$duration = sanitize_text_field($_POST[\'duration\']);
$tourContent = sanitize_text_field($_POST[\'tourContent\']);
$agancyservice = sanitize_text_field($_POST[\'agancyservice\']);
$passports = sanitize_text_field($_POST[\'passports\']);
$transport = sanitize_text_field($_POST[\'transport\']);
$hotel_name = sanitize_text_field($_POST[\'hotel_name\']);
$hotelRate = sanitize_text_field($_POST[\'hotelRate\']);
$room1 = sanitize_text_field($_POST[\'room1\']);
$room2 = sanitize_text_field($_POST[\'room2\']);
$room3 = sanitize_text_field($_POST[\'room3\']);
$room4 = sanitize_text_field($_POST[\'room4\']);
$desc = sanitize_text_field($_POST[\'desc\']);
add_complete_tour($tourTitle,$keywords,$sourceTour,$destinationTour,$duration,$tourContent,$agancyservice,$passports,$transport,$hotel_name,$hotelRate,$room1,$room2,$room3,$room4,$desc);
}
function validate_tour_data($tourTitle,$keywords,$sourceTour,$destinationTour,$duration,$tourContent,$agancyservice,$passports,$transport,$hotel_name,$hotelRate,$room1,$room2,$room3,$room4,$desc){
global $reg_errors;
$reg_errors = new WP_Error;
if (empty($tourTitle)) {
$reg_errors->add(\'tourTitle\', \'عنوان تور را وارد کنید.\');
}
if (empty($keywords)) {
$reg_errors->add(\'keywords\', \'حداقل یک کلمه کلیدی و حداکثر سه کلمه کلیدی وارد کنید.\');
}
if (empty($sourceTour)) {
$reg_errors->add(\'sourceTour\', \'مبدا و آغاز تور را وارد کنید.\');
}
if (empty($destinationTour)) {
$reg_errors->add(\'destinationTour\', \'مقصد تور را وارد کنید.\');
}
if (empty($duration)) {
$reg_errors->add(\'duration\', \'مدت اقامت را وارد کنید.\');
}
if (!is_numeric($duration)) {
$reg_errors->add(\'duration\', \'مدت اقامت فقط میتواند مقداری عددی باشد.\');
}
if (empty($tourContent)) {
$reg_errors->add(\'tourContent\', \'توضیح مختصری در مورد تور وارد کنید.\');
}
if (empty($agancyservice)) {
$reg_errors->add(\'agancyservice\', \'خدمات آژانس مسافرتی را وارد کنید.\');
}
if (empty($passports)) {
$reg_errors->add(\'passports\', \'مدارک مورد نیاز تور مسافرنی را وارد کنید.\');
}
if (empty($transport)) {
$reg_errors->add(\'transport\', \'نوع سفر را انتخاب کنید.\');
}
}
function add_complete_tour(){
global $reg_errors,$tourTitle,$keywords,$sourceTour,$destinationTour,$duration,$tourContent,$agancyservice,$passports,$transport,$hotel_name,$hotelRate,$room1,$room2,$room3,$room4,$desc;
if (is_wp_error($reg_errors) && count($reg_errors->get_error_messages()) > 0) {
echo \'<div class="alert alert-danger" role="alert" style="margin-top: 10px;font-size:15px;">\';
foreach ($reg_errors->get_error_messages() as $error) {
echo \'<strong>خطا</strong>:\';
echo $error . \'<br/>\';
}
echo \'</div>\';
} elseif (count($reg_errors->get_error_messages()) < 1) {
$post_data = array(
\'post_title\' => $tourTitle,
\'post_content\' => $tourContent,
\'post_status\' => \'pending\',
\'post_author\' => get_current_user_id(),
);
// Insert the post into the database
$post_id = wp_insert_post( $post_data );
echo \'<div class="alert alert-success" role="alert" style="margin-top: 10px;font-size:15px;">تور مسافرتی با موفقیت ثبت شد. پس از بررسی مدیریت و تایید منتشر خواهد شد.</div>\';
}
}
?>