首页 > 解决方案 > wordpress 插入帖子在边缘不起作用

问题描述

我创建了一个代码来将数据从 Form 保存到 custom_post_type。在 Firefox 上一切正常,但是当我在边缘测试时,帖子仍然保存,但我无法在管理员上编辑帖子,或查看帖子(找不到对象)(见图)

在此处输入图像描述

实在不知道哪里出了问题,求大神帮忙。

这是我的代码,我从我的 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";
    }
}

标签: phpwordpress

解决方案


这是完整的解决方案。问题主要是您没有登录 Edge 中的 WP,因此在使用wp_insert_postwordpress 创建帖子时,不知道应该使用哪个帐户附加该帖子。

    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";
    }
}

推荐阅读