Skip Navigation

[Resolved] Saving Cred Generic Fields Data & Accessing IT

This support ticket is created 7 years, 2 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.

Our next available supporter will start replying to tickets in about 1.37 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 16 replies, has 3 voices.

Last updated by Timothy 7 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#565169

I have a cpt called Wizard Sections and essentially what I’m trying to do is have a Cred form be populated by these cpt posts(into checkbox grouped by the category they are in), and then be able to output the submissions on the front-end for users. The form is working, now I'm not sure how to access the data and then output it into a template.

So far I’ve put a view into a Cred form that is outputting generic fields:

This is the View in the Cred form:

 <wpv-loop>   
          <h3 style="clear: both;">[wpv-taxonomy-title]</h3>
          [cred_generic_field field='[wpv-taxonomy-slug]' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[
[wpv-view name="website-wizard-view-1" wpvwizardelementcategory="[wpv-taxonomy-slug]"]]
}
[/cred_generic_field]

And the view in that view that is creating the options is:

<!-- wpv-loop-start --><wpv-loop>[wpv-item index=1]{"value":"[wpv-post-id]","label":"<img src=[types field='element-image' size='medium' url='true' resize='crop'][/types] >"}[wpv-item index=other] ,{"value":"[wpv-post-id]","label":"<img src=[types field='element-image' size='medium' url='true' resize='crop'][/types] >"}</wpv-loop><!-- wpv-loop-end -->

This is working and the form is rendering fine.
hidden link

But now I’m not sure how to access the submitted data so that I can put it into a template to show the user. I’ve created a custom field with the name faqs-element, the same name as one of the Cred fields, and I’ve tried this code, but it’s not working:

add_action('cred_save_data', 'custom_function',10,2);
function custom_function($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==1758)
      
    {
        $field= $_POST["faqs-element[]"];

     update_post_meta($post_id, 'wpcf-faqs-element[]', $field);      
  }
}

Thanks for your assistance,

Tim

#565313

Hi, assuming this is a "create new post" CRED form, you should get rid of the array notation "[]" and just use the field slugs. In the $_POST array, you can access it without the wpcf- prefix. When you store the value in the DB, the wpcf- prefix is required. For each item selected, you will add a non-unique row into the post meta table that matches the value selected:

add_action('cred_save_data', 'custom_function',10,2);
function custom_function($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==1758)
       
    {
        $vals= isset( $_POST["faqs-element"] ) ? $_POST["faqs-element"] : array();
        foreach($vals as $val) {
          add_post_meta($post_id, 'wpcf-faqs-element', $val, false);
        }      
    }
}

Let me know if you have any additional questions about that.

#565550

Hi Chris,

Thank you. And yes it is a Create New Post form. I'm not too swift with php but I think I get the gist of the code you suggested and added it to functions.php. But now how do I know it's being saved? And how do I use the saved data in a views template?

Tim

#565607

But now how do I know it's being saved?
You'll know it's being saved by checking in wp-admin. Edit the post that gets created by CRED. It will include the faqs element custom field, which should be populated with whatever values were selected in the generic field in your CRED form. If that's not happening, there's a problem we need to investigate further.

And how do I use the saved data in a views template?
Since you'll be saving the data in a custom field, you'll access that just like any other custom field within a Content Template. Click the "Fields and Views" button above the editor area to insert the field using our standard GUI system, or insert the field manually as described here:
https://toolset.com/documentation/customizing-sites-using-php/functions/
On the left side, select the custom field type you chose when you created the faqs-element custom field in Types. Then click the orange +More button underneath the description to see some examples.

#565804
Screen Shot 2017-09-01 at 3.12.15 PM.png

Oh, sorry, I didn't explain myself clearly. Yes, I do know how to output standard types fields. I thought this was saving it somewhere else that I needed to hook into. I did create a custom checkboxes field called faqs-element but it is still not saving anything to that.

Tim

#566521

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

As Christian is currently on vacation, I have been assigned to handle his queue.

From your last post you said you were not sure how to display the information from the field in your last post correct?

Please let me know.
Thanks,
Shane

#566607

Hi Shane. Outputting toolset custom fields I do all the time, but I don't know how to get the generic field to save to the custom field.

Tim

#566878

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

Try adding persist:1 , to your generic field.

Thanks,
Shane

#566896

I added that but it still does not work.

Tim

#566918

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

I took another look at the website and noticed that you didnt have custom fields enabled for your CPT.

Once i've enabled custom fields you should see the generic fields data showing up on the backend now.

Thanks,
Shane

#567016

Oh, I see. I thought it would be actually selecting checkboxes in the custom field on that cpt, didn't know it used WordPress custom fields. So now the issue is displaying the information. I've tried to use both these in a template to output the field but it's not yet working:

[[types field='faqs-element' option='0'][/types][types field='faqs-element' option='1'][/types][types field='faqs-element' option='2'][/types][types field='faqs-element' option='3'][/types]faqs-element]

[types field='faqs-element' separator=', '][/types]

The values this custom field is saving are the post IDs of the cpt that the generic field outputs in checkboxes (which are images). I'd like to get these images to output on the template.

Tim

#567177

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

The issue here now is that the types shortcode would only work with custom fields created using Types. In your case you will need to use this shortcode below.
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-field

Please let me know if this helps.
Thanks,
Shane

#567200

I've figured out how to output the image from the selected post, but it seems that it will only output the first selection. As this is a checkbox field multiple entries are being saved, but I'm not sure how to output those multiple entries.

This outputs the first one:
[types field='element-image' alt='%%ALT%%' title='%%TITLE%%' size='medium' align='none' resize='crop' id='[types field='faqs-element' output='raw'][/types]

I tried this to loop through and output multiple selections but it's not yet working:
<wpv-loop>
[wpv-for-each field="wpcf-faqs-element"]

  • [types field='faqs-element' separator=', '][/types]
  • </wpv-loop>

    Tim

    #567220

    Shane
    Supporter

    Languages: English (English )

    Timezone: America/Jamaica (GMT-05:00)

    Hi Timothy,

    The [wpv-for-each field="wpcf-faqs-element"] statement only works if the element is a repeated field.

    The faqs-element field was created in Types as a checkbox field correct?

    I'm a bit confused as to if this field is a Types created field or if its a field created using CRED.

    Please let me know.
    Thanks,
    Shane

    #567287

    Hi Shane,

    It's a generic field created using CRED. I originally thought if I also create a types custom field with the same name that the generic field would save to the types field. Now I see that it saves the generic field to wordpress custom fields, which is fine as long as I can access all the selected options via a template to output them on the front-end. Will that be possible?

    Tim