Home › Toolset Professional Support › [Resolved] Copying value of one field to another field?
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
This topic contains 14 replies, has 3 voices.
Last updated by Minesh 1 year, 3 months ago.
Assisted by: Minesh.
I was trying to following this advice https://stackoverflow.com/questions/53834361/copy-value-for-one-field-to-another-acf-advanced-custom-fields-wordpress
When a user submits a front-end CRED form I want the value they select here: In a field with a slug of "opportunity-removal-date" to get populated here: cred-post-expiration, but I haven't been able to get it to work using this code snippet.
I know these are Toolset based fields and not ACF but I was hoping something similar would work.
add_action('admin_footer', function () {
?>
<script type="text/javascript">
$('#cred-post-expiration').on('keyup', e => {
$('#opportunity-removal-date').val(parseFloat(e.target.value));
})
</script>
<?php
});
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Larry
Again, rather than iterating on an existing solution, let's first clarify exactly what it is that you want to do.
Your users will be updating/creating posts via front-end forms, rather than in the back-end post edit screens?
And you want to include a field "opportunity-removal-date" in the form, and have this date automatically copied across to the hidden cred post expiration field, so that the post will automatically expire at that time?
Yes that is correct! I want to include a field "opportunity-removal-date" in the form, and have this date automatically copied across to the hidden cred post expiration field, so that the post will automatically expire at that time.
Hello. Thank you for contacting the Toolset support.
You can use the Toolset Form's hook "cred_save_data" to set custom post expiration date.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Post expiration time is saved with the meta key namely "_cred_post_expiration_time" to the postmeta database table that holds the date field value as Unix Timestamp. That is why we will require to save the timestamp with the post meta key "_cred_post_expiration_time".
For example - you can add the following hook to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
add_action('cred_save_data', 'func_set_custom_post_expiration_date',10,2); function func_set_custom_post_expiration_date($post_id, $form_data){ // if a specific form if ($form_data['id']==99999){ $expiry_date = $_POST['date-field-slug']['timestamp']; update_post_meta($post_id, '_cred_post_expiration_time', $expiry_date ); } }
Where:
- Replace 99999 with your original form ID
- Replace "date-field-slug" with your original custom date field slug.
Here are few reference tickets that might help you:
- https://toolset.com/forums/topic/set-expiration-date-based-on-product-selected/
- https://toolset.com/forums/topic/post-expiry-date-in-form-saves-a-different-date-in-the-post/
I've updated the code snippet but the value is not copying.
add_action('cred_save_data', 'func_set_custom_post_expiration_date',10,2);
function func_set_custom_post_expiration_date($post_id, $form_data){
// if a specific form
if ($form_data['id']==356){
$expiry_date = $_POST['opportunity-removal-date']['timestamp'];
update_post_meta($post_id, '_cred_post_expiration_time', $expiry_date );
}
}
I will require problem URL where you added the form as well as admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
It seems you forget to share the problem URL where you added the form. Can you please share that.
Sorry, form hidden link
I know this is the form based on the ID added to the code snippet but what I would like to know is on what page/post you added the form?
Where can I see the form on frontend?
hidden link
is the form and here is an example of a published one
hidden link
I see you have first add form on this page:
- hidden link
As soon as you submit the form the user will be redirected on another form that is edit form:
- hidden link
On what setting you would like to save the post expiration date? on both form submit?
Both if possible, but at least the initial one. Thanks!
Can you please check now.
I've adjusted the code as given under to your code snipeet.
- hidden link
add_action('cred_save_data', 'func_set_custom_post_expiration_date',999,2); function func_set_custom_post_expiration_date($post_id, $form_data){ if ($form_data['id']==356 or $form_data['id']==376) { $expiry_date = $_POST['wpcf-opportunity-removal-date']['timestamp']; update_post_meta($post_id,'_cred_post_expiration_time', $expiry_date ); } }
I've also disabled the code snippet "Copy of Post Removal Date to Post Expiration Date (testing)" as it was not required.
Can you please confirm it works as expected now.
Thank you! It's very close. I think the time differences just knocks if off a little. We may have to live with that.
Its seems its because of in your WordPess settings you set your timezone for Chicago.
Is that Ok for you if we change it to UTC or you would like to live with the timezone change?