Home › Forums › General Issues › Field last update date stamp?
Is it possible in any way to get the last updated date of a specific field on a page?
In my particular use for this, I have an acf_form on the frontend. When a user inputs their name and email (the only two fields in the form) then hits save, the fields are of course updated and when the page reloads, the div with the form is hidden and a new one is unhidden. In the newly unhidden section it displays the value of the name field and a date stamp. Currently I have to manually put the date in. I am using the form as a system for clients to approve/decline graphic design mockups. I’d like to be able to pull the last update date stamp for the “name” field so it can be displayed.
I’m sure that that info is stored in the database…just don’t know how to easily pull and display it. Any thoughts?
Actually, no, this information is not saved anywhere. The date that the post was last updated in is the post database, but I don’t know this is updated when custom fields are updated https://codex.wordpress.org/Function_Reference/get_the_modified_date. First you need to do some testing to see if it’s updated and then you need to figure out a way to get the value outside of the loop because all of the function that exist for getting this value can only be used inside the loop.
Would this not be easier to have an extra text field with a custom date string in it that when the user submits the form you can use a combination of the pre_save_post filter along with the update_field() function to update this field, either to just include the default WordPress timestamp or to include your own?
https://www.advancedcustomfields.com/resources/acf-pre_save_post/
If there were other things going on on the page, or other acf form submissions that you didn’t want messing this up, you could specifically check to see if that field had been submitted by checking the post data to confirm that the name field had been changed:
function my_pre_save_post( $post_id ) {
// in this example field_0987654321 is your name field and field_1234567890 is your date field
if( $_POST['acf']['field_0987654321'] ) :
// field_1234567890 needs to be the acf slug of the date field, not the field name
update_field('field_1234567890', date('d/m/Y'), $post_id);
endif;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
Hope that helps? 🙂
I was actually able to take care of it with the following code (which is similar to your own…just left open ended for other uses than my stated one) so I could just easily display the page’s last update time stamp. Whenever the client fills in and submits the form on the front it to update the custom fields the form is tied to, it updates the page’s time stamp. Can be used for any acf_form() used actually. For some reason acf_form doesn’t do this on its own. Personally, I think it should considering its intended purpose.
// Updates FYI last update timestamp when custom field is updated
function my_acf_save_post( $post_id ) {
// bail out early if we don't need to update the date
if( is_admin() || $post_id == 'new' ) {
return;
}
global $wpdb;
$datetime = date("F j, Y g:i a");
$query = "UPDATE $wpdb->posts
SET
post_modified = '$datetime'
WHERE
ID = '$post_id'";
$wpdb->query( $query );
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
@imaginedd actually, I think it would and if I needed to test for this I would use another field and use either the hook you mentioned or the acf/save_post hook, you can accomplish the goal with either. I was focusing on “Is this information already stored somewhere?”
@imaginedd Actually… I do have one more related issue to this. Getting notified in some way when this is updated. I have tried a couple plugins that are a little older and claim to do this, but they don’t.
I’d like to be notified when a client submits the form. Whether via email, push notification…whatever. It’s the missing feature I need for this to be complete.
I am using a beta plugin which allows me to put in the acf_form() anywhere I want using a shortcode. It works really well. I have requested the ability to include optional email alerts…but ya never know if the dev will do this. https://github.com/jonathan-dejong/acf-form-shortcode
Any creative/crazy ideas?
Since email alerts for updating a post isn’t really something that acf_form() does, I honestly don’t think that Johnathan will build it in. Basically it’s outside of the focus of the plugin.
Go back to @imaginedd’s answer and see acf/pre_save_post of acf/save_post here https://www.advancedcustomfields.com/resources/acfsave_post/ and use wp_mail() to send the notification https://developer.wordpress.org/reference/functions/wp_mail/.
@hube2 Well…I’ve seen other forum threads that show how email alerts can be setup using acf_form, so it should be possible. He said he was open to suggestions…so one can hope.
As for your suggestion. I have no idea how to implement that wp_mail code correctly when it comes to integrating it with that code. I just tried and no email came. I’ve never messed that wp_mail stuff before.
Glad that you managed to resolve it, with regards to wp_mail it should be semi-straight forward
// Updates FYI last update timestamp when custom field is updated
function my_acf_save_post( $post_id ) {
// bail out early if we don't need to update the date
if( is_admin() || $post_id == 'new' ) {
return;
}
global $wpdb;
$datetime = date("F j, Y g:i a");
$query = "UPDATE $wpdb->posts
SET
post_modified = '$datetime'
WHERE
ID = '$post_id'";
$wpdb->query( $query );
$to = '[email protected]';
$subject = 'Update to ' . get_the_title($post_id);
$message = "There has been an update to your post " . $get_the_title($post_id) . "/n You can click to see this here:" . get_permalink($post_id);
wp_mail($to, $subject, $message);
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
This is untested mind, but it seems like it would work. Through using wp_mail() to send, it should follow standard site sending protocol (i.e. if you have an SMTP plugin or however your site sends by default).
Yeah…I managed to get it to send a quick email. Now I’m just messing with different variables to plugin more info.
@imaginedd Is there a trick to getting the $headers part to work? I’ve tried and tried and the emails will not send if I include the headers part. I followed the codex exactly too.
Headers can be tricky and are a common pitfall for spam filters if you are sending from an address that is different to the default site one. It should look a little bit like this however:
$headers = 'From: Edd <[email protected]>;' . "\r\n";
$headers .= 'Bcc: Admin <[email protected]>;' . "\r\n";
wp_mail($to, $subject, $message, $headers);
For those interested, below is the final code that worked. The above $headers code doesn’t work because of the double quotes, and even then, it doesn’t work. I’m guessing that the codex hasn’t been updated in a long time for that. But I managed to find a solution that works.
To recap, the use case here is where I have a custom post type setup just for having an online design approval system. I have it set to not be archived and scanned by search engines. Soon each page will only be visible to specific users who must log in. Using acf_form() I have it set to update two custom fields. Since the validation aspect of ACF doesn’t work for forms (because you’d have to fill them in when you create the page in the first place) I have set up a sort of “faux” validation. There is a hidden div that will only display if, and only if both of those fields in the form have a value. When it does, the div containing the form is hidden and the previously hidden one is unhidden. The email notification is then only triggered if, and only if both of the fields have a value.
If anyone is interested in a full demo of how it works live and would like the full code, just let me know.
// Updates FYI last update timestamp when custom field is updated and notify admin
function my_acf_save_post( $post_id ) {
// bail out early if we don't need to update the date
if( is_admin() || $post_id == 'new' ) {
return;
}
global $wpdb;
$datetime = date("F j, Y g:i a");
$query = "UPDATE $wpdb->posts
SET
post_modified = '$datetime'
WHERE
ID = '$post_id'";
$wpdb->query( $query );
$print_approved = get_field('approved_by');
$approved_email = get_field('approval_email');
$to = '[email protected]';
$subject = get_the_title($post_id) .' Design Approved';
$message = 'The design was approved by '. $print_approved .' ('.$approved_email.') to be sent to print.';
add_filter('wp_mail_from', 'new_mail_from');
add_filter('wp_mail_from_name', 'new_mail_from_name');
function new_mail_from($old) {
return '[email protected]';
}
function new_mail_from_name($old) {
return 'Spiderfly Studios';
}
if ( $approved_email && $print_approved ) {
wp_mail( $to, $subject, $message );
}
}
// run after ACF saves the $_POST['acf'] data
add_action('acf/save_post', 'my_acf_save_post', 20);
Here are a couple screenshots showing the results. Thanks to everyone who helped me to get the last few bits I needed to make this work as I needed it to!
Hello guys. I have a form that updates some special fields assigned to user accounts.
I need a field that saves (store) the current date when the form is updated, for later front-end use.
The form has the following structure:
<?php
$proprietar = get_current_user_id();
acf_form(array(
‘post_id’ => ‘user_’ .$proprietar,
‘post_title’ => false,
‘field_groups’ => array( 7214 ),
‘submit_value’ => ‘Actualizează’,
));
?>
Any idea how i can do this?
Thanks!
The topic ‘Field last update date stamp?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.