[Resolved] Save Shortcode Value in Date Generic Field
This thread is resolved. Here is a description of the problem and solution.
Problem:
When a user is editing his profile (using CRED User Edit form) the Editing date and time field needs to be automatically updated to the current date and time.
Solution:
We can only store current date in the Date field and we can not store current time in the same field. For storing current date and time in user profile, I suggest to use single line text field.
Please add this code in your theme’s or child theme’s functions.php file
I have created a user field that is Datepicker and can set both date and time. I need when a user is editing his profile the date and time of the field been automaticlly updated to the current date and time. I have created the shortcode "time-of-editing-profile" that returns current date and time with the format 'j M Y H:i'. I have inserted the shortcode in a generic field like this:
The issue is that although the shortcode outputs the right date and time when it is inserted in the generic field no date and time is set. How could I make it work? Could you please help me with that?
Thank you for contacting Toolset Support. These values are defined as JSON string, so your return output should also be in JSON. However, as you have created this custom code, we are not able to consult much on it as this is outside of our support policy.
I have not created custom code. In a previous thread you helped me with the following shortcode, so the users can see posts if the day is equal with the current day.
//Get current Local Time and Day from WP General Settings
add_shortcode('wpv-today-day', 'today_shortcode');
function today_shortcode($atts) {
$current_date_time = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ));
return strtolower(date('l', strtotime( $current_date_time)));
}
That is the shortcode that I used in the generic field. I just replaced "l" with " M Y H:i" . Is there a way to hide the date field in the cred form and automatically set the value of the field to be the current date and time with another way?
Okay I see it now, yes this shortcode I provided for the Views. So basically you are trying to "save" the date and time when a user "edit" his profile using CRED Edit User form, correct?
Let me try and see if its possible. I am working on it and will get back to you with an update.
I am able to achieve the same result with an alternate method.
1. I added a single line User field called “Time of editing profile”.
2. Modified the shortcode, please add this code in your theme’s or child theme’s functions.php file (you can change date-time format and rename the shorcode as needed):
Noman, thank you for the update! Yes I thought about this approach too but I need the field to be a datefield. I need to use it as a date and compare with other dates inside views. Could it be possible to store it as a date and not as a single line?
1. I have tried to store current date and time using Date field in CRED form, but we can only store current date in the Date field and we can not store current time in the same field. Here is updated code:
2. For storing current date and time in user profile and comparing it with another Date field, I suggest to use single line text field, storing data in date and time format, and then you can get the values from single line text field and compare it with other date field using custom code.