'Contact Form 7 & Flamingo - Getting Flamingo Post ID as a Mail tag
On a WordPress website I'm using Contact Form 7 and Flamingo to manage contact forms, and store the data.
I've been using [_serial_number] in my emails to identify a submission which is stored in Flamingo. This serial number is added to a link which opens a page, and queries the database for the submission (by serial number) to display all the information online for the user who gets the email.
I've realised today that the serial number is reset for each form you create. Eg. Form 1 submission serial numbers start at 1, and increments to 10 (for example). If I then make Form 2, submission serial numbers start at 1 again.
This is causing a problem because there are multiple posts with the same serial number, so I'm not guaranteed to get the right submission.
I can't see a way of getting the Flamingo Post ID as a mail tag anywhere, I've looked through the code for Flamingo and can't see any hooks that would let me add in the ID of the post as a Mail Tag in CF7.
Does anyone know if this is possible?
Solution 1:[1]
I solved this issue by creating and storing my own unique token with each form submission and getting Flamingo posts by that via a meta_query instead of trying to get posts by the serial_number. Unfortunately it won't solve your problem for historical submissions that lack the custom token.
- Create a field for the token in the CF7 Form tab. In my case I'm using CF7 Dynamic Text Extension to get a cookie value but it could even be static text. You just need a unique field that you can modify later.
[dynamichidden prtoken "DT_CF7_COOKIE key='dealer_id'"]
- Generate the actual token value and replace the field content with it. I used the field value + unix time + a pseudo random blob so that I can see who generated it and when but it isn't guessable. My application is relatively low risk and I'm not a security expert.
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
if( !empty( $array['prtoken'] ) ) {
//Generate real token
$prtoken = $array['prtoken'] . '_' . time() . '_' . bin2hex(random_bytes(8));
if( !empty( $prtoken ) ) {
$array['prtoken'] = $prtoken;
}
}
return $array;
};
- In my case the token forms part of a query string on a link in the email template:
<a href="https://example.com/reply-form/?serial_number=[_serial_number]&first-name=[first-name]&surname=[surname]&reply_token=[prtoken]">Reply</a>
- Get posts matching the token. Should only ever be 0 or 1 but treat it as if it might match multiple flamingo items, just in case.
$args = array(
'post_type' => 'flamingo_inbound',
'fields' => 'ids', //Only return IDS
'meta_query' => array(
array(
'key' => '_field_prtoken', //The custom token field
'value' => sanitize_text_field($field_prtoken), //The token is input by users retrieving data so needs to be sanitized.
)
)
);
$postslist = get_posts( $args );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Peter Charlton |