Skip Navigation

[Resolved] Cred save data on existing field

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

Problem: I would like to capture the value from a generic field using cred_save_data, and store it in a custom field.

Solution: Add the following code to functions.php:

add_action('cred_save_data', 'sauver_responsable_projet',10,2);
function sauver_responsable_projet($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==87)
    {
        if (isset($_POST['responsable-projet']))
        {
            update_post_meta($post_id, 'wpcf-responsable-projet', $_POST['responsable-projet'], true);
        }
    }
}

Create a generic field called 'responsable-projet' in your CRED form, and create a custom field called 'responsable-projet' in the post type associated with the CRED form.

Relevant Documentation:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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

Last updated by romanB-3 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#554395

Hello,
My CRED form includes a generic field filled thanks to a user list.
I need :
- the form to save the selected user into an existing empty field ;
- the template to show the chosen user.

I have created a custom single line text field called "responsable-projet".
The CRED form has this

        [cred_generic_field field='responsable-projet' type='select' class='' urlparam='']
        {
        "required":1,
        "validate_format":0,
        "default":[],
        "options":[json-users-list]
        }
        [/cred_generic_field]

Which is successfully filled by list of wp users thanks to this

 function wpmania_json_users_list( $atts ) {
  
    // Attributes
    extract( shortcode_atts(
        array(
            'role' => '',
            'label' => 'display_name',
            'orderby' => 'display_name'
        ), $atts )
    );
    $args = array(  'role'=>$role, 
                    'fields' => array('ID',$label),
                    'orderby' => $orderby);
    $users = get_users( $args );
    $json = json_encode($users);
    $json = str_replace('"ID"','"value"',$json);
    $json = str_replace('"display_name"','"label"',$json);
    return $json;
}
add_shortcode( 'json-users-list', 'wpmania_json_users_list' );

But I then added this code to save the chosen user to the dedicated custom field, but it didn't work :

add_action('cred_save_data', 'sauver_responsable_projet',10,2);
function sauver_responsable_projet($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==87)
    {
        if (isset($_POST['responsable-projet']))
        {
            // add it to saved post meta
            add_post_meta($post_id, '__responsable-projet', $_POST['responsable-projet'], true);
        }
    }
}

Thank you.

#554480

What do the logs say? Add some error_log statements:

add_action('cred_save_data', 'sauver_responsable_projet',10,2);
function sauver_responsable_projet($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==87)
    {
      error_log('cred save data, form 87');
        if (isset($_POST['responsable-projet']))
        {
            error_log('responsable-projet': ' . $_POST['responsable-projet']);
            // add it to saved post meta
            add_post_meta($post_id, '__responsable-projet', $_POST['responsable-projet'], true);
        }
    }
}

If you do not already have error logging set up, go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Submit the CRED form again, and find the error_log.txt file in the root of your site. Please send me its contents.

#554495

Sorry, I misread your post and my last answer was not correct. Standby, I will update you shortly.

#554496

error_log('responsable-projet' . $_POST['responsable-projet']);
[30-Jul-2017 21:26:59 UTC] responsable-projet4
Okay so we now know that the 'responsable-projet' field had a value of '4'. So the value is being sent correctly into the cred_save_data hook. It just isn't being saved correctly. As a test, let's try to modify your code to use update_post_meta instead of add_post_meta:

add_action('cred_save_data', 'sauver_responsable_projet',10,2);
function sauver_responsable_projet($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==87)
    {
      error_log('cred save data, form 87');
        if (isset($_POST['responsable-projet']))
        {
            error_log('responsable-projet' . $_POST['responsable-projet']);
            //update post meta
            update_post_meta($post_id, '__responsable-projet', $_POST['responsable-projet']);
        }
    }
}

If this does not work, then you know the problem must be related to the custom field name '__responsable-projet', because everything else is working correctly.

#554555
[31-Jul-2017 05:32:36 UTC] cred save data, form 87
[31-Jul-2017 05:32:36 UTC] responsable-projet2

I also changed "__responsable-projet" to "wpcf-responsable-projet" and "responsable-projet" with no effect...

#554737

If you manually set the field in the wp-admin area post editor, how is it stored in the database post meta table?

#554741

That I can't tell ; but here is the field's html on backend :

<div id="post-textfield-1-1501509772-wrapper" class="form-item form-item-textfield wpt-form-item wpt-form-item-textfield"><label class="wpt-form-label wpt-form-textfield-label" for="post-textfield-1-1501509772">reponsable-projet</label>
<input id="post-textfield-1-1501509772" name="wpcf[reponsable-projet]" value="" class="wpt-form-textfield form-textfield textfield textfield" data-wpt-type="textfield" data-wpt-id="wpcf-reponsable-projet" data-wpt-name="wpcf[reponsable-projet]" type="text"></div>
#554757

The names of the fields do not match. In the PHP code you have 'responsable-projet' but in the HTML markup it is 'reponsable-projet'.
Which spelling is right?
What are the settings for this custom field, and what is its slug?
Was it created in Types, or is it part of another plugin?
How is it saved in the database? You must find out. First, create a post in wp-admin and set the reponsable-projet field manually. Log in to your database using phpMyAdmin or another MySQL tool. In the wp_postmeta table, filter by the post ID associated with the post you just created. Find the row that matches the reponsable-projet meta entry, and copy the meta key. This is what you must use in your update_post_meta function.

#554867

Ow...
Thank you.
In deed there is a mistake into the field name. It has been created via Types, but now I want to edit it and Types says there is already a slug called like this, but I can't find where ?!
Thank you.

#554878

I don't have a magic solution for this, unfortunately. If the slug already exists, you cannot use it again. I suggest choosing a different slug. If you want to find where the slug is used, I suggest searching in the database wp_postmeta table.

#554898

I have just deleted all the posts that had a meta like responsable-projet.
Then I checked the wp_postmeta table and there is no "responsable-projet" nowhere.
Still I can't call my field "responsable-projet"... Why would that be ?
Thank you.

edit: Now I deleted the "reponsable-projet" (with no "s") field, saved, and can't re-create it ! It seems if I use a slug once I can't use it anymore in the future ! Even if it is not used anymore. Wouldn't that be a bug ?

#554900
Screen Shot 2017-07-31 at 4.38.57 PM.png

When you add a new field, the dialog contains a button that says "Choose from previously created fields". Is field slug you want to reuse an option when you click here? See below.

#554909

Hello,

Q1) In deed when I do this I can see the "responsable-projet", but it has the wrong type... I'd like to delete it once for all, and then create a new one with the same name !

Q2) By the meanwhile, I've created again the "reponsable-projet" (with no "s"), which is a one line text field.
I also still have my cred generic field :

[cred_generic_field field='responsable-projet' type='select' 

Now I would like to now how I should modify the function tu make it work :

add_action('cred_save_data', 'sauver_responsable_projet',10,2);
function sauver_responsable_projet($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==87)
    {
      error_log('cred save data, form 87');
        if (isset($_POST['responsable-projet']))
        {
            error_log('responsable-projet' . $_POST['responsable-projet']);
            // add it to saved post meta
            update_post_meta($post_id, 'responsable-projet', $_POST['responsable-projet'], true);
        }
    }
}

Where should I use "responsable-projet", "reponsable-projet", or even "wpcf-something" ?
Thank you.

#554915
Screen Shot 2017-07-31 at 5.00.45 PM.png

You can delete a custom field by going to Toolset > Post Fields > Post Field Control. Then you can recreate the field using the correct field type and the same slug if you want. Please see the attached screenshot that shows the Post Field Control button.

#555093

Hello,
Thank you, that worked great !

So.
My field is a one line text field, and my generic cred field is called the same : "responsable-projet"
But still, my function doesn't work :

add_action('cred_save_data', 'sauver_responsable_projet',10,2);
function sauver_responsable_projet($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==87)
    {
      error_log('cred save data, form 87');
        if (isset($_POST['responsable-projet']))
        {
            error_log('responsable-projet' . $_POST['responsable-projet']);
            // add it to saved post meta
            update_post_meta($post_id, 'responsable-projet', $_POST['responsable-projet'], true);
        }
    }
}

I've read somewhere about adding "wpcf-" before the slug ? Would that be accurate ? Where should I do this ?

Thank you.