[Resolved] PARENT_POST_TITLE not working in email send by form

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

Problem: I would like to include the parent post title in an email notification triggered by a Form that creates private child posts. Unfortunately the %%PARENT_POST_TITLE%% placeholder is not working as expected.

Solution: The best solution here is to create custom placeholders using the Forms API, and use the post relationships API in those placeholders to display information from the parent post.

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['@book-chapter_parent'];
  $newPlaceHolders = array(
    '%%FIXED_PARENT_TITLE%%' => get_the_title($parent_id),
    '%%FIXED_PARENT_LINK%%' => get_permalink($parent_id)
  );
 
  return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

Then in your notification you can use them like other placeholders:

Parent post title: %%FIXED_PARENT_TITLE%%
Parent post link: %%FIXED_PARENT_LINK%%

In your case you would change @book-chapter_parent to match your post relationship slug. Yours would be @tessuti_richiesta-info_parent. Notice that the "dot" usually seen in the post relationship slug is replaced with an underscore here.

Relevant Documentation:
https://toolset.com/documentation/user-guides/front-end-forms/how-to-use-custom-placeholders-in-cred-notifications/

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.

This topic contains 15 replies, has 2 voices.

Last updated by Silvia 1 month, 3 weeks ago.

Assigned support staff: Christian Cox.

Author
Posts
#1482511

For some reason, the %%PARENT_POST_TITLE%% placeholder doesn't work inside a notification sent by a form that adds child posts to some predefined parent posts.

#1483179

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hi, this placeholder doesn't work well in complex situations, especially if there is more than one parent post type included in the Form. When you say the parent post is predefined, do you mean the parent post Form field has a default selected value? If so, then you can include the parent post title using the Views shortcode and item attribute corresponding to the post relationship. For example, this Form creates Chapter posts (child) and there are two parents in the Form - Books and Other Parents:

Book: [wpv-post-title item="@book-chapter.parent"]<br />
Other Parent: [wpv-post-title item="@otherparentslug-chapter.parent"]<br />

The code above will include the parent Book title, then a line break, then the other parent title.

#1483309

Hello Chris, here's the situation: I have a CPT "info request" that's child to CPT "Textiles". I am making clients of the website request info about a specific fabric via a toolset form that notifies the owner of the website of each request with name, email, phone number and name of the client requesting info, as well as (of course) the fabric name (there's an archive of over 1000 fabrics!).
I created the child link form and put it into the custom template for Fabric, so that when I get to the form the fabric name is preselected.
In the notification email have inserted BOTH the placeholder %%POST_PARENT_TITLE%% and the field [wpv-post-title item="@tessuti_richiesta-info.parent"] but the results are blank nonetheless. What am I doing wrong?

#1483451

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hmm, I would need to see how the Form is configured in wp-admin. Can you share login credentials and a URL where I can see the Form on the front-end of the site?

#1487233

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

I have a custom shortcode you can use to fix this issue. Add this to your child theme's functions.php file, or to a new code snippet in Toolset > Settings > Custom code:

function toolset_get_related_post_shortcode_callback( $atts ){
  global $post;
  $atts = shortcode_atts( array(
    'post_id' => $post->ID,
    'relationship' => ''
  ), $atts );
  $relationship        = $atts['relationship'];
  $current_rfg_parent_post = toolset_get_related_post( $atts['post_id'], $atts['relationship'] );
  $out = $current_rfg_parent_post;
  return $out;
}
add_shortcode( 'toolset-related-single-post', 'toolset_get_related_post_shortcode_callback' );

Then in your notification you can use the shortcode like this:

[wpv-post-title item="[toolset-related-single-post post_id='%%POST_ID%%' relationship='tessuti_richiesta-info]"]
#1488179

Hi Chris, I tried your shortcode but the result is quite odd. I created the custom code snippet as you instructed, I pasted the shortcode "as is" in the notification body and tried the form, writing into the field the actual description of that field... this is the result:

"Il seguente utente ha richiesto informazioni sul tessuto: company name“]

Post parent link:

Form data:

What am I doing wrong?

#1489533

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Looks like I left out one step. We must register this shortcode in Toolset > Settings > 3rd-party shortcode arguments or Argomenti degli shortcode di terze parti.

I made this change for you so the shortcode should be functioning better now. Can you test once more and let me know the results?

#1489809

Hi Chris, for some reason the testing site is not sending emails. I see the connection is done correctly in the cpt admin page but I am not receiving any emails. Have u by any chance, changed anything in the form configuration?

#1490129

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

I'm seeing a server error 500 response when I test the email notification. Screenshot attached.

I saw there was a missing ' in the shortcode so I added it, but the error is not resolved. Can we get some more information about that error by turning on server logs? 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 after the WP_DEBUG line:

define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

Then use the "Send a test email" feature in this Form editor. If you see a 500 error response, this should create an error_log.txt file in your site's root directory. Please open that file in any text editor, copy its contents, and paste them in your next reply. Once that is done, you can revert the changes you made to wp-config.php and delete the log file.

#1491739

Chris, I did as you instructed and here's the content of the error_log.txt:

[04-Feb-2020 09:23:43 UTC] PHP Notice: Trying to get property of non-object in /home/sites/6a/e/ea65ea411b/adatexcopy/wp-content/toolset-customizations/nome-post-genitore.php on line 12
[04-Feb-2020 09:23:43 UTC] PHP Fatal error: Uncaught InvalidArgumentException: All provided arguments for a related element must be either an ID or a WP_Post object. in /home/sites/6a/e/ea65ea411b/adatexcopy/wp-content/plugins/types-access/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php:228
Stack trace:
#0 /home/sites/6a/e/ea65ea411b/adatexcopy/wp-content/plugins/types-access/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php(163): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(Array, 'child')
#1 /home/sites/6a/e/ea65ea411b/adatexcopy/wp-content/plugins/types-access/vendor/toolset/toolset-common/inc/public_api/m2m.php(156): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct('DUMMY_POST_ID', 'tessuti_richies...', Array)
#2 /home/sites/6a/e/ea65ea411b/adatexcopy/wp-content/toolset-customizations/nome-post-genitore.php(16): toolset_get_related_post('DUMMY_POST_ID', 'tessuti_richies...')
#3 /home/sites/6a/e/ea65ea411b/adatexcopy/wp-includes/shortcodes.php(325 in /home/sites/6a/e/ea65ea411b/adatexcopy/wp-content/plugins/types-access/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php on line 228

Waiting for your response.

#1492211

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hi, I just sent a test email using a known post ID 6294, and received the following email:

Il seguente utente ha richiesto informazioni sul tessuto: I-079

Post parent link: %%POST_PARENT_LINK%%

Form data: %%FORM_DATA%%

So the shortcode does seem to work with a known Richiesta Info post ID. Can you show me how to submit the Form on the front-end of the site so i can test with a new post?

#1492351

The form is already on the front-end. It's on the custom template for tessuti.
Just a note: not all the textiles are locked. My client wanted to keep the Gallery page on the website, so he was forced to pick a few textiles to be shown freely. But If you click on TESSUTI from the main menu and then choose one of the textiles in the filtered view (thats not show in the gallery page) it will take you to the single page for tessuto. On that page you can find the "richiedi accesso" button which is a child link to the form. Of course you need to do this on a incognito page of the browser.
I hope I answered your question!

#1492613

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay thanks for your patience, I see the difference between my setup and yours is that the child post is private. Now that I have that setting I am able to reproduce the problem in my local environment. Let me escalate this to my 2nd tier support team for more information.

#1501733

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Okay my second tier support team has informed me that there is a problem accessing related posts of private posts using the syntax I recommended in an email notification. Instead, they recommended I create a custom placeholder for you that gets the related post title and link using the post ID selected in the Form. You can then use the related placeholder in your notification. Here is an example of the custom code for creating two new placeholders:

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['@book-chapter_parent'];
  $newPlaceHolders = array(
    '%%FIXED_PARENT_TITLE%%' => get_the_title($parent_id),
    '%%FIXED_PARENT_LINK%%' => get_permalink($parent_id)
  );

  return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

Then in your notification you can use them like other placeholders:

Parent post title: %%FIXED_PARENT_TITLE%%
Parent post link: %%FIXED_PARENT_LINK%%

In your case you would change @book-chapter_parent to match your post relationship slug. Yours would be @tessuti_richiesta-info_parent. Notice that the "dot" usually seen in the post relationship slug is replaced with an underscore here.

#1501909

Thanks for your help Chris. So I created a new code snippet and edited it like you instructed. It work PERFECTLY!
What if I want to include the PARENT POST ID as well?