Skip Navigation

[Resolved] Generic field used as substitute for post_content field is not showing content

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

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 replies, has 4 voices.

Last updated by Christian Cox 3 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#580965

I am trying to: Hello I have the following problem:
The generic field I used as a substitute for the post_content and post excerpt field is not showing the content in my CRED edit form, so I can't edit it, as it's not displaying.

This is the code I used:

//post content substitute 98

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)
    {
        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
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)
    {
        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 );
        }
    }
}
                      [cred_generic_field field='post_content_substitute' type='textarea' class='sfc-campaign-new-textarea' urlparam='' placeholder='Write a few words...']
                                            {
                                            "required":1,
                                            "validate_format":0,
                                            "default":""
                                            }
                                            [/cred_generic_field]

Link to a page where the issue can be seen:
Page is under development.

I expected to see:
the words written in the CRED create content form

Instead, I got:
Nothing

#580994

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Nicholas

Generic fields in CRED forms are intended for you to be able to submit additional information along with a form that can be used by some code in the back end when the form is submitted for whatever purpose.

The expectation is that such fields are disposable.

A CRED edit form will only ever render existing standard WordPress fields and Types custom fields. If your edit form includes a generic field then the form will create that field, it won't expect to find (or even look for) an existing value in the database.

Having said that, there is a persist option to save a generic field to the database (add "persist": 1 to the options), which makes the first part of your code above redundant.

But it is still the case that a generic field in an edit form will never look for an existing value in the database.

You can use the value option to set the existing value, but you would need to write a custom shortcode to retrieve it from the database and output it as an option of your generic field.

#581013

Thank you Nigel for your fast answer.

Since this generic field will fill out both the excerpt field and post content field. I thought about the following in the cred edit form:

Just use the regular post_excerpt field, but make it required.

 [cred_field field='post_excerpt' post='product' value='' urlparam='' class='sfc-campaign-new-textarea form-control' output='bootstrap']

And then copy the changes of the post_excerpt field also into the post_content field like so

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

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

However this is not working. Do you know why?

Also what would your approach be like with the shortcode you mentioned? It sounds pretty straightforward.

Regards,
Nicholas

#581020

y.S

I assume you are using this way of gathering information because you want the post_content field to be required.

Why not just name the CRED generic field 'post_content'? Copy the value to the 'post_excerpt' the way you are doing already, if needed.

I am doing kind of the same thing on a user registration form like this:

		[cred_generic_field field='user_url' type='url' class='' urlparam='' placeholder='<em><u>hidden link</u></em>
			{
				"required":1,
				"validate_format":1,
				"default":""
			}
		[/cred_generic_field]

If you'd like to continue working the way you are already doing, you could make the code a little more simple by changing the $my_post array in the first function to:

            // add it to saved post meta
              $my_post = array(
                  'ID'           => $post_id,
                  'post_content' => $_POST['post_content_substitute'],
                  'post_excerpt' => $_POST['post_content_substitute']
              );

After that you do not need the second function for excerpt.

Hope this helps.

#581181

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Nicholas

Your code looks like it should work, though I haven't tested it.

Can you confirm what you are aiming to do here?

If your reason for doing this is to make the post_content field required, then the approach suggested by @y.S is valid.

In your form to publish the content add a required CRED generic field with the slug post_content, which will get saved in wp_posts in the post_content column.

Then in your post edit form you can either just include the normal post body field (which would show the content, but which could be deleted by your users) or include the same CRED generic field as before and for default use the post body.

You would have generic fields in your forms like so, the first from your publish post form, the second from your edit post form:

/* POST FORM */
[cred_generic_field field='post_content' type='textarea' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":"",
"persist":1
}
[/cred_generic_field]


/* EDIT FORM */
[cred_generic_field field='post_content' type='textarea' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":"[wpv-post-body view_template="None"]",
"persist":1
}
[/cred_generic_field]

If you have something different in mind let me know and I will test your code to see why it's not working.

#581208
Screen Shot 2017-10-23 at 5.13.59 PM.png
Screen Shot 2017-10-23 at 5.03.03 PM.png

Hello thank you for your responses y.S and Nigel.
Naming the CRED generic field "post_content" is really a nice approach however this gives me some weird results. (even though I deleted the former php snippets in my child-themes functions.php file for now)

On the post page these shortcodes:

<div>Post Content: [wpv-post-body view_template="None"]</div>
<div>Post Excerpt: <p>[wpv-post-excerpt format="noautop"]</p></div>

output exactly the same content even though in the backend the content is saved only in the post_content field. The post_excerpt field is empty in the backend but on the frontend the content is duplicated. (see image)

Furthermore on the frontend of the CRED edit form the words are automatically wrapped in a p tag like this (see image).

I don't understand why these things happen.

Please help.

Regards,
Nicholas

#581221

I somehow got it to work.
This worked for me:

CRED create form:

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

CRED edit form

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

+ the php snippets

//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 );
        }
    }
}

However I don't understand why this works but your solution doesn't.

Regards,
Nicholas

#581595

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Nicholas

The solution using the generic post_content field will work.

First, regarding the question of the p tags, in the CRED edit form where you use the wpv-post-body shortcode to provide the value for the post_content field, add the attribute output='raw' to prevent it from automatically adding the p tags.

Regarding the post excerpt, that's how it works. If you publish a post with post content and do not add a post excerpt, then when you come to display the non-existent post excerpt it will automatically generate a post excerpt from the post content based upon the settings you specify when using wpv-post-excerpt or using the WordPress defaults if you don't specify any settings for excerpt length etc.

The content and the excerpt look the same because your content is shorter than the cut-off length of the automatically generated excerpt. If your content were much longer you would see that the excerpt is a truncated version of the content. (See here for an explanation: https://codex.wordpress.org/Excerpt.)

#1650557

For future reference, note that the Form ID conditional here has a logic/syntax issue so the hook will actually fire for all Form submissions, not just those for Forms #98 and #216:

    // if a specific form
    if ($form_data['id']==98 || 216)

This use of the || operator will not test if the Form ID equals 98 or if the Form ID equals 216, but will test if the Form ID equals 98 or if 216 is truthy. This results in an always true condition, since 216 is a positive integer and therefore truthy. Instead, here are two alternatives that will test the Form IDs correctly:

 if ($form_data['id']==98 || $form_data['id']==216)

Or the second option, which is arguably easier to maintain as content grows:

$forms = array( 98, 216 );
if( in_array( $form_data['id'], $forms ) )

It's likely not causing a problem on the site because of the following conditional testing the existence of the substitute generic field value in the $_POST payload:

if (isset($_POST['post_content_substitute']))
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.