0

OK, I have been working on debugging this script for 2 and a half days now and am coming up empty. Basically, this is a front end form that will insert posts based on the script that I wrote for it at the top of the template page.

I am getting a blank screen every time the page loads and no debug output at all and there are no lines in debug.txt ( do have the proper settings in wp-config). Usually when I work this hard on debugging it ends up being something simple or stupid that my mind is blocked to, here is the code:

    <?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(); ?>

I have narrowed it down to the code between the opening php tag and the <?php get_header(); ?> because when I remove that section, the form loads, not styled, but it loads.

Anything obvious I am missing here?

EDIT - NEW CODE

OK, so I basically did more research and started from scratch nearly and took out nearly all of the validation for now to make things simpler.

It actually creates the page now, but there are problems...it seems as though it is not passing some of the variables to the page creation. For example, it is passing the fact that it is password protected but nothing else really - no title (it is actually blank), no page template, I believe it does actually get published. So it seems I am getting a little closer. Oh, and another issue is that once it creates a page, it redirects to another page that is actually the parent page of the page I am creating, though the URL is still the one listed above. If I hit refresh, it prompts me to resubmit form, and it will create a new page upon each refresh.

Here is the new code:

<?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(); ?>
  • Why do you add this line: do_action('wp_insert_post', 'wp_insert_post');, where do you use $link and where do you define $post_parent ? Your page_template looks strange with space and capitals, try to skip it or use the template filename instead. Check the result of wp_insert_post( $args ) with if( is_wp_error( $pid ) ) .... – birgire Apr 12 '14 at 9:49
  • + where's your nonce check and where is $post_title defined? Sorry for lengthy comment, I guess I should construct an answer from this instead. – birgire Apr 12 '14 at 10:15
  • No problem with the comment. So apparently, where I thought I had done a pretty good job in understanding what was needed to make this work, my inexperience is shining shamefully in my work. For instance, I had to look up what nonce was (seen it before, just never knew what it was) and why it was needed. I think I need to take a step back, do more research and come back at this from scratch (well, mostly anyway) – shelzmike Apr 12 '14 at 13:45
  • Sure, happens to me many times ;-) Check it out and let us know if we can help you further. I always try to start small, and then expand from there and see where it breaks - cheers – birgire Apr 12 '14 at 14:50
  • OK, so I updated the code above...closer but still having issues. – shelzmike Apr 15 '14 at 1:54
1

I can notice few problem without debugging -

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
  • Wow, I must have had tunnel-vision worse than I thought last night. I have no idea how the $new_post = array got deleted...I even had the closing ) for it and thought i checked all of those. Adding that, removing the do_action (which, btw I have no idea why this is there, I picked it up along the way researching how to do this). I also ensured the variables were assigned and the page actually displays now, but it is still not submitting properly. Thinking I might go back to the drawing board on this one, if for nothing more than to learn what the heck I am actually doing. – shelzmike Apr 12 '14 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.