Skip Navigation

[Resolved] Include parent custom field value in form notification

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

Problem:
Include parent custom field value in form notification

Solution:
You can use the Toolset form hooks to add parent field using custom place holder:
- cred_body_notification_codes
- cred_subject_notification_codes

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/include-parent-custom-field-value-in-form-notification/#post-1966263

Relevant Documentation:
- https://toolset.com/documentation/programmer-reference/forms/how-to-use-custom-placeholders-in-cred-notifications/

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Silvia 3 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1966005

I am developing a Real Estate website and I want to give the user the ability to request informations about a certain property. I created CPT “INFO REQUEST” , the relationship between INFO REQUEST and PROPERTIES and proceeded to create a form in order to allow customers to request info about a certain property.

I am working on the web owner notification, and, besides the INFO REQUEST form data, I would like to add informations on what property the user wants to know more about, so I chose to insert Property name and Reference number.

In the form notification I tried using %%POST_TITLE%% but since it wasn’t working I followed instructions on one of my own previous tickets, which is this: https://toolset.com/forums/topic/parent_post_title-not-working-in-email-send-by-form and now I can retrieve both Property post title and property post link.
As additional info tho, I would like to add Property reference number which is a custom field in the PROPERTY cpt.
I read this documentation: https://toolset.com/documentation/programmer-reference/forms/how-to-use-custom-placeholders-in-cred-notifications/ to create custom placeholders in cred notifications and proceeded as follows:

A) created the generic field in the form :

   [cred_generic_field field='rifimmobile' type='hidden' class='' urlparam='']
          {
          "required":0,
          "validate_format":0,
          "default":"[types field='riferimento-immobile' item='@immobile-richiesta-info.parent'][/types]"
          }
          [/cred_generic_field]

B) inserted the following in functions.php

add_filter('cred_body_notification_codes', 'custom_generic_field_notification');
add_filter('cred_subject_notification_codes', 'custom_generic_field_notification');

function custom_generic_field_notification( $defaultPlaceHolders ) {
  $parent_id = $_REQUEST['@immobile-richiesta-info_parent'];
  $newPlaceHolders = array(
    '%%FIXED_PARENT_TITLE%%' => get_the_title($parent_id),
    '%%FIXED_PARENT_LINK%%' => get_permalink($parent_id),
	'%%RIF_IMMOBILE%%' => $_REQUEST['rifimmobile']
  );

  return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

but the placeholder %%RIF_IMMOBILE%% returns blank. Can you point to what I am doing wrong? Thank you!

#1966263

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Assuming that you get a correct $parent_id. We will try to fetch the parent post field using the parent ID.

Can you please try the following code.

add_filter('cred_body_notification_codes', 'custom_generic_field_notification',10,1);
add_filter('cred_subject_notification_codes', 'custom_generic_field_notification',10,1);
 
function custom_generic_field_notification( $defaultPlaceHolders ) {
  $parent_id = $_REQUEST['@immobile-richiesta-info_parent'];
  $rifimmobile  =  get_post_meta($parent_id,'wpcf-riferimento-immobile',true);
  $newPlaceHolders = array(
    '%%FIXED_PARENT_TITLE%%' => get_the_title($parent_id),
    '%%FIXED_PARENT_LINK%%' => get_permalink($parent_id),
    '%%RIF_IMMOBILE%%' => $rifimmobile
  );
 
  return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

If above code works you should remove the generic field "rifimmobile" you added to your form as its not required as we are fetching the value from database.

#1966365

Dear Minesh, I tried but I got this error when trying to save the file functions.php:

"Le tue modifiche al codice PHP sono state annullate a causa di un errore alla linea 437 del file wp-content/plugins/cred-frontend-editor/application/views/dialogs/shortcodes/cred-relationship-form.phtml. Correggi e prova a salvare nuovamente.

Uncaught ValueError: Unknown format specifier "S" in wp-content/plugins/cred-frontend-editor/application/views/dialogs/shortcodes/cred-relationship-form.phtml:437
Stack trace:
#0 wp-content/plugins/cred-frontend-editor/application/views/dialogs/shortcodes/cred-relationship-form.phtml(437): sprintf('Add %1$S to the...', '{{data....', '{{data....')
#1 wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/files.php(99): include('/home/sites/6a/...')
#2 wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/renderer/renderer.php(136): Toolset_Files->get_include_file_output('/home/sites/6a/...', Array)
#3 wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/inc/autoloaded/renderer/renderer.php(110): Toolset_Renderer->render_phtml_template(Object(Toolset_Output_Template_Phtml), NULL)
#4 wp-content/plugins/cred-frontend-editor/application/controllers/shortcode_generator.php(942): Toolset_Renderer->render(Object(Toolset_Output_Template_Phtml), NULL)
#5 wp-content/plugins/cred-frontend-editor/application/controllers/shortcode_generator.php(916): CRED_Shortcode_Generator->render_dialog_templates()
#6 wp-includes/class-wp-hook.php(287): CRED_Shortcode_Generator->render_footer_dialogs('')
#7 wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)
#8 wp-includes/plugin.php(484): WP_Hook->do_action(Array)
#9 wp-admin/admin-footer.php(78): do_action('admin_footer', '')
#10 wp-admin/theme-editor.php(385): require_once('/home/sites/6a/...')
#11 {main}
thrown
Ignora"

Searching on the internet I found out that this might be a problem caused by PHP 8.0.
I reverted the php version to 7.4 and saved again, then I tried your code and it works perfectly.
When I replied I hit "I still need assistance" because I was just sending you the error I got from saving, but this ticket can in fact be closed!

#1966405

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please try the following:

add_filter('cred_body_notification_codes', 'custom_generic_field_notification',9,1);
add_filter('cred_subject_notification_codes', 'custom_generic_field_notification',9,1);
  
function custom_generic_field_notification( $defaultPlaceHolders ) {
  $parent_id = $_REQUEST['@immobile-richiesta-info_parent'];
  $rifimmobile  =  get_post_meta($parent_id,'wpcf-riferimento-immobile',true);
  $newPlaceHolders = array(
    '%%FIXED_PARENT_TITLE%%' => get_the_title($parent_id),
    '%%FIXED_PARENT_LINK%%' => get_permalink($parent_id),
    '%%RIF_IMMOBILE%%' => $rifimmobile
  );
  
  return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

If above does not work, please share admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1966421

Minesh, your code was right the first time around.. maybe you read my message before I could edit and update it with the new infos I had by searching the error in the internet.

Everything works fine now, thank you so much. I assume with this code I can retrieve any custom field I want from a parent and use it in the form notification, which is super useful.
Thank you!