dsfe167834 2019-03-06 10:45
浏览 43
已采纳

wordpress插入帖在边缘不起作用

I create a code to save data from Form to custom_post_type. Everything ok on Firefox but when I test it on edge, the post still save, but I can't edit the post on admin, or view the post(object not found) (see the image)

enter image description here

I really don't know where is the problem, please help.

this is my code, i copy it from my 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";
    }
}
  • 写回答

1条回答 默认 最新

  • duanqian9503 2019-03-07 09:57
    关注

    Here is the complete solution. The issue mainly is you are not logged into the WP in Edge so when creating the post using wp_insert_post wordpress has no idea with which account should it attach that post.

        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";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导