WP_INSERT_POST脚本和表单中的错误,可能是我看不到的简单错误

时间:2014-04-12 作者:shelzmike

好的,我已经调试这个脚本两天半了,现在空了。基本上,这是一个前端表单,它将根据我在模板页面顶部为其编写的脚本插入帖子。

每次加载页面时,我都会看到一个空白屏幕,根本没有调试输出,调试中也没有任何行。txt(在wp config中有正确的设置)。通常,当我努力进行调试时,结果是一些简单或愚蠢的事情,我的大脑被阻止了,下面是代码:

    <?php
/*
Template Name: Create Group Pages
*/
?>

<?php
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // Do some minor form validation to make sure there is content
    if (isset($_POST[\'submit\'])) {
            $error = "";

        if (!empty($_POST[\'title\'])) {
            $title = $_POST[\'title\'];
     } else {
        $error .= "Please add a title<br />";
    }

        if (!empty($_POST[\'cat\'])) {
            $post_category = $_POST[\'cat\'];
     } else {
        $error .= "Please select a school.<br />";
    }

        // ADD THE FORM INPUT TO $new_post ARRAY
        if (empty($error)) {
            \'post_content\'      =>  \'\',/**MUST STAY BLANK!**/
            \'post_name\'         =>  $post_title,
            \'post_title\'        =>  wp_strip_all_tags( $_POST[ \'post_title\' ] ),
            \'post_status\'       =>  \'publish\',
            \'post_type\'         =>  \'page\',
            \'ping_status\'       =>  \'closed\',
            \'post_parent\'       =>  $post_parent, /**Needs to be changed to capture data from parent selections**/
            \'post_password\'     =>  \'12345\',
            \'comment_status\'    =>  \'closed\',
            \'post_category\'     =>  wp_strip_all_tags( $_POST[ \'cat\' ] ),
            \'page_template\'     =>  \'Teacher Group Page Template\'
        );

        //SAVE THE POST
        $pid = wp_insert_post($new_post);

        //REDIRECT TO THE NEW POST ON SAVE
        $link = get_permalink( $pid );

        } // END SAVING POST
    } // END VALIDATION
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST NOW
do_action(\'wp_insert_post\', \'wp_insert_post\');
?>

<?php get_header(); ?>

            <div class="content formcontainer" role="main">

                    <div class="form-content">
                     <?php
                        if (!empty($error)) {
                            echo \'<p class="error"><strong>Your page was NOT created<br/> The following error(s) returned:</strong><br/>\' . $error . \'</p>\';
                        } elseif (!empty($success)) {
                            echo \'<p class="success">\' . $success . \'</p>\';
                        }
                    ?>                      

        <!-- CLASS GROUP CREATION FORM -->

        <div class="new-pg-form">
        <form id="new_post" name="new_post" method="POST" action="" enctype="multipart/form-data">
            <!-- post name -->
            <fieldset name="name">
                <label for="post_title">Post Title:</label>
                <input type="text" id="post_title" value="" tabindex="5" name="post_title" />
            </fieldset>

            <!-- post Category -->
            <fieldset class="category">
                <label for="cat">School:</label>
                <?php wp_dropdown_categories(\'orderby=name&hide_empty=0&exclude=1&hierarchical=1\'); ?>
            </fieldset>

            <!-- post Parent -->
            <fieldset class="parent">
                <label for="post_parent">Pick the school this class is from:</label>
                <?php $parent_args = array( \'depth\' => 1 , \'child_of\' => \'1094\'); wp_dropdown_pages( $parent_args ); ?>
            </fieldset>

            <fieldset class="submit">
                <input type="submit" value="Create Page" tabindex="40" id="submit" name="submit" />
            </fieldset>

            <input type="hidden" name="action" value="new_post" />
            <?php wp_nonce_field( \'new-post\' ); ?>
        </form>
        </div> <!-- END new-pg-form -->

        <!-- END OF FORM -->
                </div><!-- #form-content-->
            </div><!-- .content .formcontainer -->      

<?php get_footer(); ?>
我已经把它缩小到了开始php标记和<?php get_header(); ?> 因为当我删除该节时,表单将加载,而不是样式化,而是加载。

有什么明显的我遗漏的吗?

EDIT - NEW CODE

好的,所以我基本上做了更多的研究,几乎从零开始,现在几乎完成了所有的验证,使事情变得更简单。

它实际上现在创建了页面,但存在一些问题。。。似乎它没有将一些变量传递给页面创建。例如,它传递了这样一个事实,即它受密码保护,但实际上没有其他内容-没有标题(实际上是空的),没有页面模板,我相信它确实得到了发布。看来我离得有点近了。哦,另一个问题是,一旦它创建了一个页面,它就会重定向到另一个页面,而这个页面实际上是我正在创建的页面的父页面,尽管URL仍然是上面列出的URL。如果我点击刷新,它会提示我重新提交表单,每次刷新时它都会创建一个新页面。

以下是新代码:

<?php
/*
Template Name: Create Group Pages
*/
?>

<?php

$post_title_error = \'\';

if ( isset( $_POST[\'submitted\'] ) && isset( $_POST[ \'post_nonce_field\' ] ) && wp_verify_nonce( $_POST[ \'post_nonce_field\' ], \'post_nonce\') ) {

    if ( trim( $_POST[\'post_title\'] ) === \'\' ) {
        $post_title_error = \'Please enter a title.\';
        $has_error = true;
    }

}

$new_post = array(
    \'post_content\'      =>  \'<?--All handled by template file, no need to type anything here -->\',/**MUST STAY BLANK!**/
    \'post_name\'         =>  wp_strip_all_tags( $_POST[ \'post_title\' ] ),
    \'post_title\'        =>  wp_strip_all_tags( $_POST[ \'post_title\' ] ),
    \'post_status\'       =>  \'publish\',
    \'post_type\'         =>  \'page\',
    \'ping_status\'       =>  \'closed\',
    \'post_parent\'       =>  wp_strip_all_tags( $_POST[ \'post_parent\' ] ), /**Needs to be changed to capture data from parent selections**/
    \'post_password\'     =>  \'12345\',
    \'comment_status\'    =>  \'closed\',
    \'post_category\'     =>  wp_strip_all_tags( $_POST[ \'cat\' ] ),
    \'page_template\'     =>  \'Teacher Group Page Template\'
    );

    $new_pid = wp_insert_post( $new_post );

    if ( $new_pid ) {
        wp_redirect( home_url());
        exit;
    }
?>


<?php get_header(); ?>

<?php if ( $post_title_error != \'\' ) { ?>
    <span class="error"><?php echo $post_title_error; ?></span>
    <div class="clearfix"></div>
<?php } ?>

<form action="" id="create-class-groups-form" method="POST">

    <fieldset>
        <label for="post_title">Post Title:</label>
        <input type="text" name="post_title" id="post_title" class="required" value="<?php if ( isset( $_POST[\'post_title\'] ) ) echo $_POST[\'post_title\']; ?>" />

    </fieldset>

    <!--The School is really category in WP terms, school is what the cat is representing needs to be changed-->
        <h3>School:</h3>
        <?php wp_dropdown_categories(\'orderby=name&hide_empty=0&exclude=1&hierarchical=1\'); ?>

    <!--This area determines the parent so that it takes proper place in line-->
        <h3>What Class is this school from:</h3>
        <?php $parent_args = array( \'depth\' => 1 , \'child_of\' => \'1094\'); $post_parent = wp_dropdown_pages( $parent_args ); ?>

    <fieldset>
        <input type="hidden" name="submitted" id="submitted" value="true" />
        <?php wp_nonce_field( \'post_nonce\', \'post_nonce_field\' ); ?>
        <button type="submit">Add Class</button>
    </fieldset>

</form> <!-- #create-class-groups-form -->

<?php get_footer(); ?>

1 个回复
SO网友:Shazzad

如果不进行调试,我可以注意到一些问题-

if (empty($error)) {
    $new_post = array( // this line is missing
        \'post_content\'      =>  \'\',/**MUST STAY BLANK!**/

// this line should not be there
do_action(\'wp_insert_post\', \'wp_insert_post\');

// variable undefined
$post_title | $post_parent | $success

结束

相关推荐