Change Post time when editing a post created with Create Post

By: Reinaldo Gonzalez | Asked: 02/17/2023
ForumsCategory: How-toChange Post time when editing a post created with Create Post
Reinaldo Gonzalez asked 1 year ago
Hi! I'm trying to edit the post time when editing a "Create Post" (Actions & Notifications) entry. I can edit the date by changing the post date, but by default, it creates the Post with the time 00:00 (12:00 AM). There is a workaround in FF documentation, but it seems to work only when creating a new Post, not when editing it and trying to reschedule it for another date/time. Thanks!
3 Answers
Walter JonesWalter Jones answered 1 year ago
This is a tricky one…the publish time is set by default on submit using the post action “published date” and as you know it’s not updated on edit.  You may have to use a hook frm_after_update_entry to try and make this work.  

My thoughts are create a text field (name it Publish Time) and give it a format of 9999-99-99 99:99:99 (way the time is formatted in Wordpress on the date/time) and give that field a custom css class of publish-time-field (used to target the function). Then put this code in a code snippet:
function update_post_publish_time($entry_id, $form_id) {
  if ($form_id == YOUR_FORM_ID_HERE) { // Replace YOUR_FORM_ID_HERE with the ID of your Formidable form
    $publish_time = FrmProEntriesController::get_field_value_shortcode(array('field_id' => PUBLISH_TIME_FIELD_ID, 'entry' => $entry_id));
    $post_id = FrmProEntriesController::get_post_id_by_key($entry_id);
    $post = get_post($post_id);
    if ($post) {
      $post->post_date = $publish_time;
      $post->post_date_gmt = get_gmt_from_date($publish_time);
      wp_update_post($post);
    }
  }
}
add_action('frm_after_update_entry', 'update_post_publish_time', 10, 2);
Make sure you change the Form ID and Publish Time Field ID in the code above with your respective IDs. 

In theory this will update the publish time on the post after you make an edit to the entry.  Keep in mind I haven’t tested this code yet, so user beware. Hope It gets you somewhere closer you need to be.  
Reinaldo Gonzalez replied 1 year ago

Hi Walter. Thank you for taking the time to review it and provide me with solutions. I'm getting an error on line 5 when running it.
Thanks.
Reinaldo

Reinaldo Gonzalez replied 1 year ago

Error: Uncaught Error: Call to undefined method FrmProEntriesController::get_post_id_by_key()

Phil Rips answered 1 year ago
@Reinaldo Gonzalez did you solve this? I can't see why FF is not supporting the Published Time for Creating/Updating posts. This would not be hard to implement given most of the logic is already in place. It's pointless future dating posts if you can't set the time from a UX perspective.
Reinaldo Gonzalez replied 1 year ago

Hi Phil, still working on it. I asked Formidable Forms twice already, and I got the "we'll evaluate if this is an option that would benefit a high number of users" answer every time.
Best,
Reinaldo

Phil Rips replied 1 year ago

Thanks for the update. I don't think it's an option per se, it's a defect in the original solution. There is no point in future dating a post without setting the time - the whole point of future dating posts is to reach a particular audience at a specific date and time. FF need to finish what they started as this is a premium feature. 🙂

Reinaldo Gonzalez answered 1 year ago

Hello, Here is the code I'm testing, and so far works with "Edit" and "Duplicate" Posts. It allows changing both the Date and Time of a post. The "date_field_id_1" is for the new date, "date_field_id_2" is for the new time. It comes from ChatGPT after many changes and adjustments. I hope it helps those in need.

 

add_action('frm_after_create_entry', 'change_post_time_from_date_fields', 30, 2);
add_action('frm_after_update_entry', 'change_post_time_from_date_fields', 30, 2);  
function change_post_time_from_date_fields($entry_id, $form_id) {    

// Get the post ID associated with the Formidable Forms entry    
$entry = FrmEntry::getOne($entry_id);    
$post_id = $entry->post_id;     

// Get the value of the date fields that contain the new post date and time    
$new_post_date = FrmProEntriesController::get_field_value_shortcode(array('field_id' => 'date_field_id_1', 'entry' => $entry_id));    $new_post_time = FrmProEntriesController::get_field_value_shortcode(array('field_id' => 'date_field_id_2', 'entry' => $entry_id));      

// If the post ID and new date and time are set, update the post date and time    
if ($post_id && $new_post_date && $new_post_time) {        
$new_post_date_time = date('Y-m-d H:i:s', strtotime($new_post_date . ' ' . $new_post_time));        
$new_post_date_time_gmt = get_gmt_from_date($new_post_date_time);          

// Update the post date and time with the new date and time        
$post_date = substr($new_post_date_time, 0, 19);        
$post_date_gmt = substr($new_post_date_time_gmt, 0, 19);          

$post = array(            
'ID' => $post_id,            
'post_date' => $post_date,            
'post_date_gmt' => $post_date_gmt,        

);          

wp_update_post($post);    

}

}

Reinaldo Gonzalez replied 1 year ago

Note that in my case, my time field format is H:I, not H:I:s, as shown above. So check that your time field format matches the format above; otherwise, it does not work. Also, it requires a hidden field with [post_id] as the default value.

Phil Rips replied 1 year ago

Thanks for the code, I will give it a try sometime. Perhaps we can send this to Formidable Forms so they can fix the defect in the solution. 🙂

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right