Skip Navigation

[Resolved] I need to implement a cred child notification

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

Problem: I would like to access the name of the page where my CRED form is included, in the post created by the CRED form. The page where the form is shown has no parent/child relationship to the post created by CRED.

Solution: Use a generic field in your CRED form to capture the current page ID. Use the page ID to get the page title, then update your post's meta information to include the page title.

[cred_generic_field field='currentpageid' type='textfield' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-id]"
}
[/cred_generic_field]
add_action('cred_save_data', 'save_parent_title',10,2);
function save_parent_title($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==550)
    {
        if (isset($_POST['currentpageid']))
        {
            $parent_title = get_the_title($_POST['currentpageid']);
            update_post_meta($post_id, 'wpcf-field-name', $parent_title, true);
        }
    }
}

Relevant Documentation: https://toolset.com/documentation/user-guides/cred-api/#csd
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 23 replies, has 3 voices.

Last updated by oriolc 7 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#495029

Finally I understand you. Sorry about that.
I've created a new custom field called titol-formulari-pare with type's custom field to the child custom post.
After I insert in the form:
[cred_generic_field field='wpcf-titol-formulari-pare' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"generic_type":"post_title",
"default":"[wpv-post-title]"
}
[/cred_generic_field]

And call with types shortcode for the notification, but it appears the child title, not the parent title

#495191

Shane
Supporter

Languages: English (English )

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

Hi Oriol,

Ok so for the post title.

It seems that we may need to do some juggling in order to get it to work.

Use this hook here to set the Parent post title.

add_action('cred_save_data', 'save_parent_title',10,2);
function save_parent_title($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        if (isset($_POST['my_custom_field']))
        {
            $parent_id = get_post_meta($post_id, '_wpcf_belongs_enviar-formulario_id');
            $parent_title = get_the_title($parent_id);
            update_post_meta($post_id, 'wpcf-titol-formulari-pare', $parent_title, true);
        }
    }
}

Now remove the generic field from your form for the Parent title and try adding this hook to your functiosn.php file.

Replace the 12 with the ID of your form.

Please let me know if this helps.

Thanks,
Shane

#495653

I made the changes you asked me, but the message appears with the child title.....

add_action('cred_save_data', 'save_parent_title',10,2);
function save_parent_title($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==550) // 550 is enviar-formulario cred. I tested with parent cred, and won't change.
{
if (isset($_POST['my_custom_field']))
{
$parent_id = get_post_meta($post_id, '_wpcf_belongs_enviar-formulario_id');
$parent_title = get_the_title($parent_id);
update_post_meta($post_id, 'wpcf-titol-formulari-pare', $parent_title, true);
}
}
}

#495805

Hi, Shane is off today and I have been assigned to cover this ticket. I hope that's okay with you. I see in the code you posted this line:

if (isset($_POST['my_custom_field']))

I think Shane meant that you need to replace 'my_custom_field' with the actual slug of your field. Your custom field is not actually called 'my_custom_field', correct?

#495955

Ok,
I change my_custom_field for titol-formulari-pare, but I still obtain the child title. I need the parent title.
Thanks.

#496246

Hi, I'm a bit confused. You posted the following code:

// if a specific form
if ($form_data['id']==550) // 550 is enviar-formulario cred. I tested with parent cred, and won't change.

However, your custom field is not in this form (form ID 550):
hidden link

Since the field is not in this form, your title does not get updated. Is it possible you added the custom field to a different form?

#496523

Ok, I change the function:
add_action('cred_save_data', 'save_parent_title',10,2);
function save_parent_title($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)
{
if (isset($_POST['nombre-retiro-formulario'])) <- This field is in the form 550, and is set to send the message.
{
$parent_id = get_post_meta($post_id, '_wpcf_belongs_enviar-formulario_id');
$parent_title = get_the_title($parent_id);
update_post_meta($post_id, 'wpcf-titol-formulari-pare', $parent_title, true);
}
}
}

But nothing change. It appears the child title, not the parent title.

#496789

Hi, I have been a bit confused but I think I understand now. I looked at your site to understand why this is not working. Shane and I thought that you are using parent / child post relationships, but your CRED form does not create a child post. Let me explain.

Your Evento Gratuito post is here:
hidden link

The CRED form is on this page. It creates an Enviar formularios post. The Enviar formularios post it creates is NOT a child of the Evento Gratuito post. The Enviar formularios post it creates has no relationship to the current page - which is why the parent post ID does not show up correctly with the code we recommended. I think the problem is that you have not set up the CRED form to create a parent / child relationship.

So I made some changes for you.

You need to know the ID of the page where the CRED form is shown. So I added this generic field to your CRED form:

[cred_generic_field field='currentpageid' type='textfield' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-id]"
}
[/cred_generic_field]

Now when the form is submitted, you can access that custom currentpageid field. I changed your functions.php file to use this new field to get the page title:

add_action('cred_save_data', 'save_parent_title',10,2);
function save_parent_title($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==550)
    {
        if (isset($_POST['currentpageid']))
        {
            $parent_title = get_the_title($_POST['currentpageid']);
            update_post_meta($post_id, 'wpcf-titol-formulari-pare', $parent_title, true);
        }
    }
}

Please review this code. First we check the form ID. Only form 550 will be effected. Then we check if the currentpageid custom field is set. If it is, we update the wpcf-titol-formulari-pare field on the Enviar formularios post to include the title of the current page. Then, the notification that is sent can access this information in the email template.

Please test these changes and the notifications they create. I added my email temporarily and sent a few tests, and now I believe everything is set up correctly. Please let me know if I can be of further assistance.

#497168

Thank you very much!!!!
At the end, you can solve me to send the message with the title of the current page.
And yes, I think that I won't explained well the post and cred form website parent/child relationship. I just can say, sorry about that and thank you another time!!!!!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.