Ask your WordPress questions! Pay money and get answers fast! (more info)

Set time stamp on post from front end WordPress

  • SOLVED

The solution to this problem should be straightforward but it has alluded me thus far.

I need the three date fields in the form below to set the time stamp in the new post.
<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data" style="margin-bottom:15px">
<p>
<input name="title" id="title" type="text" />
<input type="submit" value="Add New Group" id="submit" name="submit" />
</p>
<p>
<textarea id="description" name="description"></textarea>
</p>
<p>Day: <input name="day" id="day" type="text" />
Month: <input name="month" id="month" type="text" />
Year: <input name="year" id="year" type="text" />
</p>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>


Header script:

/*****************************************/
/* Setup our post from front end */
/*****************************************/
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['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a new group name';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
$description = $_POST['&nbsp;'];
}

// if (isset ($_POST['date'])) {
// $postdate = $_POST['Y-m-d'];
// }
//else {
// $postdate = $_POST['2011-12-21 18:57:33'];
// $postdate = date( $_POST['2011-12-21 18:57:33'] );
// }

// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_date' => $postdate,
'post_status' => 'publish',
'post_type' => 'album',
'post_parent' => $parent_id,
'post_author' => get_current_user_id(),
);

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

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

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');


Using $postdate = date('2010-02-23 18:57:33'); has not worked.

Answers (3)

2011-12-23

Francisco Javier Carazo Gil answers:

Hi Matt,

You want that your post have this date instead of real date.

Try this:

$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];

$postdate = $year . "-" . $month . "-" . $day . " 00:00:00";


And in $post remember also gmtdate:

'post_date' => $postdate,
'post_date_gmt' => $postdate,


Francisco Javier Carazo Gil comments:

Hi Matt,

My answer should work. Have you tried it?


Matt Taylor comments:

Both you and Hai came up with the same answer within a few minutes of each other, so I voted to split the pot evenly.

Thank you!

2011-12-23

Hai Bui answers:

Hi,

I think the 'post_date' parameter should be a string. So instead of

$postdate = date('2010-02-23 18:57:33');
You should use
$postdate = '2010-02-23 18:57:33';

The full code:

/*****************************************/

/* Setup our post from front end */

/*****************************************/

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['title'])) {

$title = $_POST['title'];

} else {

echo 'Please enter a new group name';

}

if (isset ($_POST['description'])) {

$description = $_POST['description'];

} else {

$description = $_POST['&nbsp;'];

}

$postdate= $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'].' 00:00:00';

// ADD THE FORM INPUT TO $new_post ARRAY

$new_post = array(

'post_title' => $title,

'post_content' => $description,

'post_date' => $postdate,

'post_status' => 'publish',

'post_type' => 'album',

'post_parent' => $parent_id,

'post_author' => get_current_user_id(),

);



//SAVE THE POST

$pid = wp_insert_post($new_post);



//REDIRECT TO THE NEW POST ON SAVE

// $link = get_permalink( $pid );

// wp_redirect( $link );



} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM



//POST THE POST YO

do_action('wp_insert_post', 'wp_insert_post');


Matt Taylor comments:

A hard coded date is working, but using the _POST variables is returning a "Page not found" error. instead of refreshing the page the post was created on.

Any thoughts on what could be wrong? Of course I disabled every plugin on the site.


Hai Bui comments:

Please try printing $postdate to see if it is correct:


$postdate= $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'].' 00:00:00';
echo $postdate;


Matt Taylor comments:

Figured it out, it was a theme function causing the issue.

Thank you!

2011-12-23

Julio Potier answers:

Hello

I think other people solved the problem, but i want to paste you the codex info and tell you to visit the codex often. :

[[LINK href="http://codex.wordpress.org/Function_Reference/wp_insert_post"]]http://codex.wordpress.org/Function_Reference/wp_insert_post[[/LINK]]

$post = array(
'ID' => [ <post id> ] //Are you updating an existing post?
'menu_order' => [ <order> ] //If new post is a page, sets the order should it appear in the tabs.
'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
'ping_status' => [ 'closed' | 'open' ] // 'closed' means pingbacks or trackbacks turned off
'pinged' => [ ? ] //?
'post_author' => [ <user ID> ] //The user ID number of the author.
'post_category' => [ array(<category id>, <...>) ] //Add some categories.
'post_content' => [ <the text of the post> ] //The full text of the post.
<strong> 'post_date' => [ Y-m-d H:i:s ] //The time post was made.
'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT.</strong>
'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs.
'post_name' => [ <the name> ] // The name (slug) for your post
'post_parent' => [ <post ID> ] //Sets the parent of the new post.
'post_password' => [ ? ] //password for post?
'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //Set the status of the new post.
'post_title' => [ <the title> ] //The title of your post.
'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //You may want to insert a regular post, page, link, a menu item or some custom post type
'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags.
'to_ping' => [ ? ] //?
'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies.
);

in <strong>"bold"</strong> you can see the codex help, this are string, just formated dates, not a "date()" return.

See you !