Archived topic
Postmeta email link for CPT using element->block->Content Template
11 replies · Started by Jeff on September 13, 2021
I'm creating an archive page for a custom post type; ACF for custom fields.
There's a field named "contact_email".
I can successfully add a dynamic headline to display the email address.
But when I try to create a mailto link, the system adds "http://" followed by the email address — instead of "mailto:".
Am I doing something wrong?
Is there a workaround? How can I get GP Premium to use mailto?
Thanks.
Hi there,
In your dynamic link, make sure it's set to #.
Then it should pull in the full mailto:you@you.com if you add it like that to the custom field.
So the system will replace href="#" with href="your-custom-field-value".
Let me know :)
This worked for me. Thank you!
No problem!
I'm a bit unsure about getting this to work.
My ACF field is set as email type for validation, thus including 'mailto:' within the field content is prohibited. How should I set this up to enable this for the email and also for a phone field for 'tel:'?
We would need to filter the output in that case.
For example:
add_filter( 'generate_dynamic_element_url', function( $url, $link_type, $source, $block ) {
if ( 'post-meta' === $link_type ) {
$meta_name = $block['attrs']['gpDynamicLinkCustomField'];
if ( $meta_name && 'your-email-meta-field-name' === $meta_name ) {
$url = 'mailto:' . $url;
}
if ( $meta_name && 'your-tel-meta-field-name' === $meta_name ) {
$url = 'tel:' . $url;
}
}
return $url;
}, 10, 4 );
Thanks Tom,
As always you came through with the goods, works a treat!
Much appreciated
Glad I could help! :)
Hi Tom,
I followed all your instructions, but did not achieve success. Regardless that it works straightforward to create nice author boxes, firstly, thanks for that ^.^/
The Link button is set to #, the dynamic option (Link Type) is set to Author Meta and the Field Name is set to user_mail.
Nevertheless, the button does not pull any mail afterwards, only the root domain.
Do you have any ideas what this could be? All other links work (like LinkedIn).
Thanks in advance,
Chris
Hi there,
did you update the meta names in Toms code here ?
Hey David, thank you for the reply!
Good hint.
I have now added the code to the customizer as follows (I hope this is the right approach):
add_filter( 'generate_dynamic_element_url', function( $url, $link_type, $source, $block ) {
if ( 'post-meta' === $link_type )
$meta_name = $block['attrs']['gpDynamicLinkCustomField'];
if ( $meta_name && 'user_email' === $meta_name ) {
$url = 'mailto:' . $url;
}
}
return $url;
}, 10, 4 );
The box is still pulling the domain. I will try it again later though :)
Let us know how you get on!