Skip Navigation

[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

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 $current_date_time;
}

Then in the CRED Edit User form, you can add this field as below:

<div class="cred-field cred-field-time-of-editing-profile">
        <label class="cred-label"> Time of editing profile </label>
   
[cred_field field='time-of-editing-profile' post='user' value='[wpv-today-day]' urlparam='' readonly='true' placeholder='[wpv-today-day]' class='form-control' output='bootstrap']
    </div>

Alternate solution can be:
https://toolset.com/forums/topic/save-shortcode-value-in-date-generic-field/#post-530398

This support ticket is created 7 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by ioannisM-2 7 years, 5 months ago.

Assisted by: Noman.

Author
Posts
#530273

Hello,

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:

[cred_generic_field field='user-field-for-date-and-time' type='date' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"default":"[time-of-editing-profile]"
}
[/cred_generic_field]

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!

#530302

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ioannis,

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.

More info:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field

We have some recommended list of service providers here if you would like to take a look: https://toolset.com/consultant/

Have a great day, Thank you

#530328

Hello Noman,

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?

Thank you!

#530366

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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.

#530398

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Ioannis,

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):

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 $current_date_time;
}

3. Then in the CRED Edit User form, you can add this field as below:

<div class="cred-field cred-field-time-of-editing-profile">
		<label class="cred-label"> Time of editing profile </label>
  
[cred_field field='time-of-editing-profile' post='user' value='[wpv-today-day]' urlparam='' readonly='true' placeholder='[wpv-today-day]' class='form-control' output='bootstrap']
	</div>

==> And if you would like to hide this field, change its class as: class='form-control hidden'

Thank you

#530460

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?

Thank you

#530517

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Ioannis,

I have tried to add the date field, but it is not storing current date. I will look for a workaround and get back to you with an update.

Thank you for your patience.

#530583

Ok, thank you Noman!

#530903

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Ioannis,

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:

add_shortcode('wpv-today-day', 'today_shortcode');
function today_shortcode($atts) {
	return  current_time( 'timestamp', 0 );
}

- Then in the CRED Edit User form, you can add this field as below:

[cred_field field='time-of-editing-profile' post='user' value="[wpv-today-day]" placeholder='[wpv-today-day]'  urlparam='' readonly='true' class='form-control' output='bootstrap']

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.

Thank you

#531115

I will try and if I do not manage to achieve that , I will use the date field only. Thank you Noman!