Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Trying to work out how I could track the time that a field was updated and display that time on a page.

So far:

Using advanced custom fields and the options page the user updates some fields

What I'd like to be able to do

Capture the time this happens and output it onto a page

Why?

Each day the user is inputting prices (which effect all prices across the site) and would like a line that reads "prices correct as of ..."

In Summary

So to sum it up, is it possible to track when this custom field is updated? Or any other ideas for tracking this event? I know I could put an extra field underneath where the user could input todays date and time but I'm trying to set up an automated failsafe system for them.

I don't have to use advanced custom fields and I'm welcome to other ideas, but that's what is set up currently so I was looking to integrate into it.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can use the acf_update_value filter to set a post meta value whenever the field is updated.

Here's an example for a field named test_field. Check if the current field is test_field, then get its old value and compare it to the new value. If it has been updated, update a post meta field named _update_time with the current time. In your template, use get_post_meta to output the time.

function wpa85599_acf_update_value( $value, $field, $post_id ){
    $old_val = get_field( 'test_field', $post_id );
    if( $old_val != $value )
        update_post_meta( $post_id, '_update_time', time() );

    return $value;
}
add_filter( 'acf_update_value-test_field', 'wpa85599_acf_update_value', 10, 3 );

Have a look through the ACF documentation for other filters and actions. There are also more general filters that run on all fields or just certain field types, as welll as this filter which is specific to a single field name.

EDIT-

I just noticed you said options page, not a post. To make this work with the options page, just change update_post_meta to instead update an option:

update_option( 'update_time', time() );
share|improve this answer
    
Thanks @Milo, this seems to be the right way to do it however I can't get it to work. The filter works right, I can set it to change the value of the current field by changing $value = 'this' (I did this to make sure I was filtering the right field) but I can't get update_option to change any other fields, even ones currently in use. Any ideas why? –  Paul Tibbetts Feb 13 '13 at 15:55
    
the update_option example updates your own option, not a field created by ACF. If you want to update an ACF option field, you would reference it by prefixing the field name with _options_. FYI, you can see all of the data stored in your options table by manually visiting the admin URL /wp-admin/options.php. –  Milo Feb 13 '13 at 16:15
    
actually, my last comment is incorrect, sorry. it appears the options table just stores a reference to the field name where the data is stored, I believe in the post meta table somewhere. the simplest solution is to just store it in your own custom option, then access the value in your template via get_option. I'll dig around later and see how ACF actaully stores the options data. –  Milo Feb 13 '13 at 16:22
    
Thanks @Milo, really appreciate the help. FWIW update_option is changing other options (eg. worked on blogdescription) but not on any ACF options eg. options_update_time or _options_update_time. EDIT Whilst writing this I've just tried creating a WP option and referencing it and it's working. My fault here, I wasn't clear when I said I was on the options page I meant on the ACF options page, completely forgot about the WP options. Thanks for taking the time to look into this. –  Paul Tibbetts Feb 13 '13 at 18:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.