Skip Navigation

[Resolved] Generic field default value wrong after upgrading to CRED version 1.9.4

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

Problem: I have a generic field in a CRED edit form. This field should be populated with the value of a custom field. The code I had in place before updating to CRED 1.9.4 was working, but now the generic field is not set correctly. Now, the generic field is showing the excerpt of the current page rather than the excerpt of the post being edited.

Solution:
Add explicit context to the CRED form to ensure the correct post values are being displayed. In this particular case, the editing post ID is passed into the CRED form using a URL parameter. We will use the same URL parameter to supply context to the generic field value shortcode:

[cred_generic_field field='post_content_substitute' type='textarea' class='new-textarea' urlparam='' placeholder='Write a few words...']
{
  "required":1,
  "validate_format":0,
  "default":"[wpv-post-excerpt format='noautop' id='[wpv-search-term param='post_id']']",
  "persist":1
}
[/cred_generic_field]
This support ticket is created 6 years, 10 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by Nicholas 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#607298
Screen Shot 2018-01-17 at 7.14.19 PM.png

Hello so I just upgraded to CRED version 1.9.4 and noticed the following bug:

My CRED create form has a generic field called post_content_substitute.
The content in that field gets submitted to the posts post_content and post excerpt field.

A user has the option to select the status of the post i.e.: save as draft or publish for review.

After upgrading I tried to save a post as a draft. The content of the generic field is submitted to the posts post_content and post excerpt field but then when I try to edit the post on the frontend with a CRED edit form the shortcode of the CRED edit form is inside the post_content_substitute field. (see image)

This issue just came with the update to CRED version 1.9.4 I don't know why. Everything used to work just fine before.

Please help. It's kind of frustrating.

#607300

The funny thing is that when I then save the draft again with the CRED edit form the shortcode is submitted to the post_content and post excerpt field and the original text is all gone.

I just went back to version 1.9.3.1

#607657

Hi, I will be glad to help but I need more information.
- Is the behavior the same with a default theme and only Toolset plugins enabled? If not, can you isolate one component that seems to be causing the conflict?
- Can you share the code contents of both CRED forms, as well as any relevant CRED API or other PHP code on the backend?
- Can you explain in detail how the "Edit" CRED form is added to the site - is it placed in a View, or in a Content Template or Template Layout, is it in a custom Page, etc.?

#610304

Hello, I am currently still using version 1.9.3.1, because I don't have a lot of time dealing with that issue. Sorry for the late reply.

Here are the code components:

/*******************************************************/
//create form &&  edit form
/*******************************************************/


//post content substitute 98 && 216

add_action('cred_save_data', 'my_save_content',10,2);

function my_save_content($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==98 || 216)
    {
        if (isset($_POST['post_content_substitute']))
        {
            // add it to saved post meta
              $my_post = array(
                  'ID'           => $post_id,
                  'post_content' => $_POST['post_content_substitute']
              );
 
            // Update the post into the database
              wp_update_post( $my_post );
        }
    }
}


//copy post content substitute content to excerpt field 98 && 216
add_action('cred_save_data', 'my_save_excerpt',10,2);
function my_save_excerpt($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==98 || 216)
    {
        if (isset($_POST['post_content_substitute']))
        {
            // add it to saved post meta
              $my_post = array(
                  'ID'           => $post_id,
                  'post_excerpt' => $_POST['post_content_substitute']
              );
 
            // Update the post into the database
              wp_update_post( $my_post );
        }
    }
}




//validate form characters 98 && 216

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data){
    //field data are field values and errors
    list($fields,$errors)=$field_data;
        
    //validate if specific form
    if ($form_data['id']==98 || 216){   
    
if( ($_POST['post_status']!='draft') ) {      
        //check my_field value
        if (strlen(trim($fields['post_content_substitute']['value'])) < 250 ){
            //set error message for my_field
            $errors['post_content_substitute']='Please enter at least 250 characters in Step 3 or save your post as a draft to edit it later';
        }      
    }
}        
    //return result
    return array($fields,$errors);
}



And:
Create Form

[cred_generic_field field='post_content_substitute' type='textarea' class='new-textarea' urlparam='' placeholder='Write a few words...']
                                            {
                                            "required":1,
                                            "validate_format":0,
                                            "default":""
                                            }
                                            [/cred_generic_field]

Edit Form:

 [cred_generic_field field='post_content_substitute' type='textarea' class='new-textarea' urlparam='' placeholder='Write a few words...']
                                            {
                                            "required":1,
                                            "validate_format":0,
                                            "default":"[wpv-post-excerpt format="noautop"]",
                                            "persist":1
                                            }
                                            [/cred_generic_field]

And here are the related tickets from the past I've created:

https://toolset.com/forums/topic/generic-field-used-as-substitute-for-post_content-field-is-not-showing-content/

https://toolset.com/forums/topic/make-excerpt-fields-required/

https://toolset.com/forums/topic/cred-post-excerpt-and-post-content/

- Can you explain in detail how the "Edit" CRED form is added to the site - is it placed in a View, or in a Content Template or Template Layout, is it in a custom Page, etc.?

I created a page called: Form Edit

And placed this shortcode in the post_content field [cred_form form='form-edit' form_name='Form Edit' post='[wpv-search-term param="post_id"]']

Hope this can help you to solve the problem.

I am trying to set up a staging site soon.

Regards,
Nick

#610471

Okay thanks, it sounds like the wpv-post-excerpt shortcode is now inferring the wrong post context:

"default":"[wpv-post-excerpt format="noautop"]"

I say that because it is not outputting the correct post's excerpt here - it's outputting the excerpt of the current page instead of the post being edited, if I understand you correctly. You have added an explicit post ID to the post_content field's cred_form shortcode using the "wpv-search-term" shortcode to resolve a similar issue:

[cred_form form='form-edit' form_name='Form Edit' post='[wpv-search-term param="post_id"]']

So first, I suggest adding the same explicit post reference to the generic field's default value post excerpt shortcode like this:

[cred_generic_field field='post_content_substitute' type='textarea' class='new-textarea' urlparam='' placeholder='Write a few words...']
                                           {
                                           "required":1,
                                           "validate_format":0,
                                           "default":"[wpv-post-excerpt format='noautop' id='[wpv-search-term param='post_id']']",
                                           "persist":1
                                           }
                                           [/cred_generic_field]

This should help apply the proper context to the wpv-post-excerpt shortcode. If this is not working as expected, please add the wpv-post-excerpt and wpv-search-term shortcodes just after the generic field shortcode so we can debug their output.

Test Post ID: [wpv-search-term param='post_id']<br />
Test Excerpt: [wpv-post-excerpt format='noautop' id='[wpv-search-term param='post_id']']

Are the ID and excerpt rendered as expected after the generic field?

#610481

Hello Christian. Wuhuu you're the best. This is solved.

However, now I noticed another issue :/

I have this field [cred_field field='product_cat' display='select' single_select="true"] placed two times in a single cred edit form.

The right category is saved to the wp backend when creating the post, however on the frontend of the CRED edit form the wrong category is selected.

This doesn't happen with version 1.9.3.1. Again I am going back to this version.

Regards,
Nick

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/split-bug-after-upgrading-to-cred-version-1-9-4/

#610528

Okay sure, I've created another ticket where we can track that separately from this issue. Thanks for the report.

#611480

This is solved.