Skip Navigation

[Resolved] Show default value for Fields

This thread is resolved. Here is a description of the problem and solution.

Problem

I want to show default values for my fields, in case the field is not edited when saving the post (is empty).
I am editing and creating posts with CRED.

Solution

There are 2 possible ways to achieve this, one while adding the value of the Field, the other when displaying the field on the front end.

1. While adding the field value (edit the field on the front end thru a CRED form)

- create the custom fields and include them in the CRED Form
- pass a default value to them by using the "value" attribute

==> Since you had set a default value, the field will always show that default value if not changed
==> You can achieve the same by setting the default value in Toolset > Post Fields directly

Or, you can skip the above steps and update the Custom Field (if empty) with a custom hook to

update_post_meta()

with your default value, using the

cred_save_data()

filter

2. When outputting the field value on a post

- create the custom fields and include them in the CRED Form
- submit the cred form with eventually empty optional fields
- display the fields as used to, but wrap them in an HTML conditional

In the conditional, check if the field is empty to display your custom default text, or if it's not empty you can display the value of the field.

This support ticket is created 6 years, 9 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Author
Posts
#621602

Tell us what you are trying to do?
I am to get a automatic text for all empty fields which are not required. so if a field is not filled, I want to display on frontend "info not provided". Example - there is a text field address. If user or contributor or form filler does not fill that box, I want it to show "info not provided" when the form/ post is posted.
Is there any documentation that you are following?
no
Is there a similar example that we can see?
no
What is the link to your site?
hidden link

#621692

There are 2 possible ways to achieve this, one on input, the other on output.

1. On Input

- create the optional custom fields and include them in the CRED Form
- pass a default value to them by using the "value" attribute:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field
- then just display the Field in the front end as used to.

==> Since you had set a default value, the field will always show that default value if not changed
==> You can achieve the same by setting the default value in Toolset > Post Fields directly
==> Or, you can as well update the Custom Field (if empty) with a custom hook to update_post_meta() with your default value, using the cred_save_data() filter:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

2. on Output

- create the optional custom fields and include them in the CRED Form
- submit the cred form with eventually empty optional fields
- display the fields as used to, but wrap them in an HTML conditional:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/

In the conditional check if the field is empty if it displays your custom default text, if not, the field.

Please let me know if anything remained unclear.

#622231

I could not understand. My field is a text field. How can I have value for that? Can you provide some screenshot?

#622523
Bildschirmfoto 2018-03-06 um 07.46.30.png

In the CRED Form when you autogenerate the form, your Field will show thru a ShortCode.
that ShortCode also has a "value" attribute.

There you insert your default value.
See also screenshot.

Or, as I elaborated, you can output this with an HTML Conditional.

Or, you can update the field with an API code.

Please, let me know which method you would like to approach.

It depends a lot on what you need to do with those empty fields.
If you just need to display a certain value if empty but not actually save that value, you can use HTML conditional.

If you need to save the default value but you do not care if the user actually edits it, you can simply use the "value" attribute as shown in the screenshot.

If you need in any case either the user-submitted value or the default value, you will need to approach the above-explained custom code API call.

#623567

Thanks for help. My problem is resolved.