Skip Navigation

[Resolved] Access Post ID in a CRED notification custom placeholder

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

Problem: I would like to access the Post ID in the callback for a CRED email notification custom placeholder.

Solution:
You can access the Post ID in the defaultPlaceHolders array that is passed into the callback functions:

// custom cred form placeholders
function custom_generic_field_notification( $defaultPlaceHolders ) {
     
    $post_id = $defaultPlaceHolders['%%POST_ID%%'];

     
    //generate random mail id
    $random_mail_id = get_post_meta($post_id, 'random-mail-id', true);
    if (empty($random_mail_id)) {
        $random_mail_id = bm_ajax_generate_random_mail_id($post_id);
        update_post_meta($post_id, 'random-mail-id', $random_mail_id);
    }
    $random_mail_id = '?mid='.$random_mail_id; 
 
    $newPlaceHolders = array(
        '%%mid%%' => $random_mail_id
    );
 
    return array_merge($defaultPlaceHolders, $newPlaceHolders );
}
add_filter('cred_subject_notification_codes', 'custom_generic_field_notification', 10, 1);
add_filter('cred_body_notification_codes', 'custom_generic_field_notification', 10, 1);

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

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

Last updated by tinaH 5 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#872086

I am trying to create a cred notification code and I need to request the post-id to generate a value.
The post id field is not in the form...
Is this possible?

// custom cred form placeholders
function custom_generic_field_notification( $defaultPlaceHolders ) {
    
    $post_id = $_REQUEST['-----------how can I get the post-id here?-------------'];
    
    //generate random mail id
    $random_mail_id = get_post_meta($post_id, 'random-mail-id', true);
    if (empty($random_mail_id)) {
        $random_mail_id = bm_ajax_generate_random_mail_id($post_id);
        update_post_meta($post_id, 'random-mail-id', $random_mail_id);
    }
    $random_mail_id = '?mid='.$random_mail_id; 

    $newPlaceHolders = array(
        '%%mid%%' => $random_mail_id
    );

    return array_merge($defaultPlaceHolders, $newPlaceHolders );
}
add_filter('cred_subject_notification_codes', 'custom_generic_field_notification', 10, 1);
add_filter('cred_body_notification_codes', 'custom_generic_field_notification', 10, 1);
#873939

Since there is already a %%POST_ID%% placeholder, you can access the post ID in the default placeholders array that is passed into the callback:

$post_id = $defaultPlaceHolders['%%POST_ID%%'];
#875756

You have no idea how happy that makes me 🙂 !!

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