Published date fix for front end editor help needed

By: Chris Andrews | Asked: 04/19/2022
ForumsCategory: Code HelpPublished date fix for front end editor help needed
Chris Andrews asked 3 years ago

Hi all,

I've got my site set up with the goal of handling all post management through the front end editor.

I ran into a problem in that, when a post is published through the front end editor, the published time is set to the original time the post was saved as a draft, not the actual time the post status is changed to 'published'.

Formidable Forms was kind enough to give me a snippet to fix this issue, it's below.

The problem I've run into however, is that it only corrects the time when a post is changed from 'draft' to 'published'.

My users save the post as pending to let me know it's ready for a review. If I review the post and publish it from the front end editor - so the post is going from 'pending' to 'published', the time is not corrected to the date of publication.

I suspect this snippet needs a minor adjustment to including pending > published as well, but I've been playing with it and haven't been about to figure it out.

Any help would be appreciated!

Chris

//=====================================================//

//FF sets the published time as the time the post was initially saved as draft. This snippet

//changes that to the time of publication:

global $my_frm_post_drafts;

$my_frm_post_drafts = array();

function update_post_timestamp_on_status_update( $entry_id ) {

global $my_frm_post_drafts;

if ( array_key_exists( $entry_id, $my_frm_post_drafts ) ) {

$postarr = array(

'ID' => $my_frm_post_drafts[ $entry_id ],

'post_date' => gmdate( 'Y-m-d H:i:s' )

);

wp_update_post( $postarr );

}

}

add_action( 'frm_after_update_entry', 'update_post_timestamp_on_status_update' );

function check_values_before_update( $new_values, $entry_id ) {

$status_field_id = 15; // change this to status field id from submission form

$meta = $new_values['item_meta'];

if ( isset( $meta[ $status_field_id ] ) && 'publish' === $meta[ $status_field_id ] ) {

$post_id = FrmDb::get_var( 'frm_items', array( 'id' => $entry_id ), 'post_id' );

if ( $post_id ) {

$post = get_post( $post_id );

if ( $post && 'draft' === $post->post_status ) {

global $my_frm_post_drafts;

$my_frm_post_drafts[ $entry_id ] = $post_id;

}

}

}

return $new_values;

}

add_filter( 'frm_pre_update_entry', 'check_values_before_update', 10, 2 );

2 Answers
Best Answer
Victor Font answered 3 years ago

If you change one line, it should get you what you need:

Change:

if ( $post && 'draft' === $post->post_status )

To:

if ( $post && ( 'draft' === $post->post_status || 'pending' === $post->post_status ) )

Chris Andrews answered 3 years ago

Yep, that did the trick, thank you Victor!

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