Skip Navigation

[Resolved] Turn a WYSIWYG field into a multiple lines field in form

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

Problem: I would like to display a multi-line input field instead of the standard WYSIWYG post content field in a Form that creates posts.

Solution: Use a generic multiline field and delete the standard post content field. Use the cred_save_data hook to replace the post content with the generic field content.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 3 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 6 replies, has 2 voices.

Last updated by rensD 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1649413

Tell us what you are trying to do?
Use a form to edit the content of a post. Instead of the standard WYSIWYG that is automatically set by the cred_field I want to input with a multiple lines field.

The form is displayed on:
hidden link
In the dashboard the form is called add_solution under post forms and the field is called post_content.

#1649427

There's nothing built-in to the software that will do this automatically. If you'd like to use a custom code solution, you could delete the post content input area from the Form and use a generic multiline field to capture the User input, then use the cred_save_data hook in some custom code to set the User's input as the post content. Something like the code submitted by the User in this ticket:
https://toolset.com/forums/topic/generic-field-used-as-substitute-for-post_content-field-is-not-showing-content/

Let me know if you have questions about this.

#1649461

So instead of this:

[cred_field field='post_content' class='form-control' output='bootstrap']

I use this:

[cred_generic_field type='textarea' field='post_content']
          {
          "required":0,
          "default":""
          }
 [/cred_generic_field]

From the ticket you linked I still don't fully understand how to create a "cred_save_hook".

#1649463

Please change the generic field slug to be something other than post_content for clarity, since the original field's slug is post_content. Then to create a cred_save_data hook, you must write PHP code like the PHP code shared by the other User. You can place that code in a child theme's functions.php file just before the last line, or you can create a new custom code snippet in Toolset > Settings > Custom Code. The snippet should be set to run on the front-end, and must be activated.

#1649477

I got it to work! Thanks!

So I got this:

[cred_field field='post_content_lines' class='form-control' output='bootstrap']

With:

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_lines']))
        {
            // add it to saved post meta
              $my_post = array(
                  'ID'           => $post_id,
                  'post_content' => $_POST['post_content_lines']
              );
  
            // Update the post into the database
              wp_update_post( $my_post );
        }
    }
}

Not sure what the id == 98 or 216 is about... but it works

#1650539

Okay great! Nice work. I reopened the ticket though because of a couple of things I need to point out:

1
----------
Not sure what the id == 98 or 216 is about... but it works
I recommend you change this now. It looks like the original author was trying to limit the code to execute on only two Forms: ID 98 and ID 216. But the code doesn't actually work as intended. There is a syntax/logic issue that will result in the code firing on ALL Forms submissions...let me explain. All cred_save_data hooks are fired any time any Form is submitted, so a conditional like this can help determine whether or not the code is applied to the correct Form(s). So if you add 3 more Forms later, this code will be triggered for all 4 Forms. That can cause unexpected problems, so a conditional like this will help limit the scope of this custom code. I suggest you change this line to check for the proper Form ID, or it could cause problems later for other Forms and it's better to do this now than to try to remember in the future.

So please go to Toolset > Post Forms and find the numeric ID of the Form you are modifying (it's next to the Form title). Then modify that line of code to apply the proper Form ID. If the Form ID is 1234, for example, the line should be:

if ( $form_data['id']==1234 )

If you plan to apply this code to more than one Form (for example, if you create an edit post form later), then instead of the errant code in the other ticket you should set up a list of Form IDs (in PHP this is called an array) in the first line, then test the submitted Form ID against the contents of that array using the PHP function in_array:

$forms = array( 1234, 5678 ); // replace with a comma-separated list of Form IDs where you want to apply this code
if( in_array( $form_data['id'], $forms ) ) // checks to see if the submitted Form's ID is in the array

Replace 1234, 5678 with a comma-separated list of all the Form IDs where you want to apply this custom code.

2
------------
my_save_content
This function name is perfectly fine from a functionality perspective, but you might notice that the same sample function name is used in many other code examples on the site. If you happen to copy + paste the same function name again in another custom code example, it will cause a PHP fatal error and crash your site. So it's best to go ahead and choose a unique function name every time you create a custom code snippet. For example in this case you could call it set_post_content_with_generic_multiline and update your code in both places here:

add_action('cred_save_data', 'set_post_content_with_generic_multiline',10,2);
function set_post_content_with_generic_multiline($post_id, $form_data)

Again, the snippet will work now with the function name you already have in place...but it's a good idea to change it sooner rather than later.

#1650801

Thanks for the addition.

I now have:

add_action('cred_save_data', 'set_post_content_with_generic_multiline',10,2);
 
function set_post_content_with_generic_multiline($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==1371)
    {
        if (isset($_POST['post_content_lines']))
        {
            // add it to saved post meta
              $my_post = array(
                  'ID'           => $post_id,
                  'post_content' => $_POST['post_content_lines']
              );
  
            // Update the post into the database
              wp_update_post( $my_post );
        }
    }
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.