• Hey fellas,

    I’m working on a WordPress Web site for a student newspaper, and I’m running into a few snags trying to build a “Letter to the Editor” form. I’ve made a custom template, letters.php, with a PHP form embedded in it, and a “Letters” page that uses the template. Here’s the form I’ve got:

    <?php
    	$my_fancy_new_post = array();
    		$my_fancy_new_post['post_title'] = $_POST['post_title'];
    		$my_fancy_new_post['post_content'] = $_POST['post_content'];
    		$my_fancy_new_post['comment_status'] = 'closed';
    		$my_fancy_new_post['post_author'] = 2;
    		$my_fancy_new_post['post_category'] = array(30);
    		$my_fancy_new_post['post_status'] = 'draft';
    
    	$writer_name = $_POST['letter_name'];
    	$writer_school = $_POST['school'];
    	$writer_year = $_POST['letter_year'];
    	$writer_email = $_POST['email'];
    	$writer_phone = $_POST['telephone'];
    
    	if (!isset($_POST['submit'])) {
    ?>
    
    <form method="post" action="">
    	<table>
    		<tr><th><label for="id_name">Your Name(s):</label></th><td><input id="id_name" type="text" name="letter_name" maxlength="100" /></td></tr>
    		<tr><th><label for="id_school">School:</label></th><td><input id="id_school" type="text" name="school" maxlength="100" /></td></tr>
    		<tr><th><label for="id_year">Year:</label></th><td><input id="id_year" type="text" name="letter_year" maxlength="50" /></td></tr>
    		<tr><th><label for="id_email">E-Mail:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
    		<tr><th><label for="id_telephone">Telephone:</label></th><td><input id="id_telephone" type="text" name="telephone" maxlength="16" /></td></tr>
    
    		<tr><th><label for="id_title">Letter Title:</label></th><td><input id="id_title" type="text" name="post_title" maxlength="100" /></td></tr>
    		<tr><th><label for="id_text">Your Comments:</label></th><td><textarea id="id_text" rows="10" cols="40" name="post_content"></textarea></td></tr>
    		<tr>
    			<td>&nbsp;</td>
    			<td><input type="submit" value="Submit" name="submit"/></td>
    		</tr>
    	</table>
    </form>
    
    <?php
    	} else {
    		echo '<p>';
    		$new_post_ID = wp_insert_post( $my_fancy_new_post );
    		if( $new_post_ID ) {
    			add_post_meta($new_post_ID, 'writer', $writer_name);
    			add_post_meta($new_post_ID, 'letter_school', $writer_school);
    			add_post_meta($new_post_ID, 'letter_year', $writer_year);
    			add_post_meta($new_post_ID, 'letter_email', $writer_email);
    			add_post_meta($new_post_ID, 'letter_phone', $writer_phone);
    			echo 'Your letter was submitted successfully. Thank you for contacting <em>The Cavalier Daily!</em>';
    		}
    		else {
    			?>
    				An error occured while submitting your letter. Please go back and try again.</p>
    				<p>Potentially useful debug information:<br />
    				<strong>$_POST['post_title']</strong> <?php echo $_POST['post_title']; ?><br />
    				<strong>$_POST['post_content']</strong> <?php echo $_POST['post_content']; ?><br />
    			<?php
    			}
    		echo '</p>';
    	}
    ?>

    When I try the form, I get the error condition. The debug information – the title and content – both seem to be working fine, and they’re the only information the plugin collects that goes into wp_insert_post. As far as I can tell, all the information I’m supplying manually checks out, so why isn’t it working?

    I tried using TDO Mini Forms, but I was essentially getting the same error – wp_insert_post was returning 0 with it as well. Are there any known plugins or server settings that interfere with that function?

    Thanks in advance for your help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • anmari

    (@anmari)

    For the record, I had the same problem and figured out that one must ensure that the post title is unique, otherwise wp_insert_post will fail with return code 0.
    So although data entering a unique title works, the DB shows many posts with same title, and can seem to use standard add new post with same titles, so not sure still exactly what the problem is.

    anmari

    (@anmari)

    On further investigation in wp3.0 at least, wp_insert_post calls function wp_unique_post_slug sorts out the uniqueness issue. Testing today it seems it is just a blank title for a published post inserted via calling wp_insert_post that does not work. (Publishing with a blank title via add new post menu is okay). I guess that is easier to deal with !

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problems with wp_insert_post in POST form’ is closed to new replies.