I would like to create a function for community members to be able to contact the author of a specific post. I don't want the post author's email address to be visible to the person contacting so a contact form would be required as opposed to a mailto link. The site uses "WordPress Form Maker" for its main contact form currently and it also uses "Post SMTP" to send and log emails.
Hello, you can use Toolset Forms to create a contact Form. I would create a new custom post type called "Contacts" or "Inquiries" or something similar, add any custom fields you want to include in the notification email, and you can add a one-to-many post relationship between the original post and the new post type, where the original post is the parent and the new post type is the child, if you want to make a connection between the original post and its Contact/inquiry posts. Then create a new Toolset Post Form that creates new Contact/Inquiry posts.
When you create this Form, insert in the Form Builder a generic hidden field. Be sure to give it a slug, and set the default value for this field to be the original post author's ID using the following shortcode:
[wpv-post-author format="meta" meta="ID" item="$current_page"]
That will set the current post's author ID in the generic hidden field if the Form is displayed in the Content Template for the original post type. Set up an email notification in this Form, and use the recipient option "Send notification to a WordPress user with an ID coming from a generic field in the form:". Select your generic hidden field slug in this field to complete the settings, and the email notification will be sent to the original post author automatically.
Let me know if you have questions about this approach.
Thanks for this, I'm part way through setting this up to test it. In the same way that [wpv-post-author format="meta" meta="ID" item="$current_page"] gets the authors ID, is there similar code for getting the post title and the post URL?
In the same way that [wpv-post-author format="meta" meta="ID" item="$current_page"] gets the authors ID, is there similar code for getting the post title and the post URL?
Yes, of course. Use the item attribute in the wpv-post-title or wpv-post-url shortcodes to get the original post information:
Title: [wpv-post-title item="$current_page"]
URL: [wpv-post-url item="$current_page"]
Documentation for these and other shortcodes is available in our programmer reference. I'm also including a link to documentation about the item attribute, used to specify an explicit post context for Views and Types shortcodes:
Views shortcodes: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/
Types field shortcodes: https://toolset.com/documentation/customizing-sites-using-php/functions/
Item attribute: https://toolset.com/documentation/customizing-sites-using-php/functions/item-attribute/
I think I've managed to make this work but wanted to check something...
Is there a list of generic field shortcodes somewhere so that I can call them in the notification section? I thought it might be the "Form data" in the placeholders menu but that kept returning nothing. I had initially included a generic multi line text box for a message but in the end got round it by creating a custom field for the new post type which I could then select from the "Fields and views" menu.
Is there a list of generic field shortcodes somewhere so that I can call them in the notification section?
I assume you mean you want to include information from one or more generic fields in the notification body or subject. The best solution in that case is to create custom placeholders for these fields, as described here:
https://toolset.com/documentation/programmer-reference/forms/how-to-use-custom-placeholders-in-cred-notifications/
An example User ticket:
https://toolset.com/forums/topic/include-custom-generic-fields-added-to-the-form-in-the-notification-email/
If you're trying to use a generic field as a notification recipient, see this other example:
https://toolset.com/forums/topic/notification-to-an-email-specified-in-an-email-field-in-form-is-not-working/
Thanks again, that seems rather complex for something I assumed would be a standard feature! Thanks for the resources though, I'll stick with the custom field for now and if I need more generic fields I'll tackle this later.
I'm having trouble getting the following to return any information.
[wpv-post-title item="$current_page"]
I think it worked once, weirdly, but now doesn't seem to. Does it need a corresponding hidden field like the id? I assumed that was only to use it in the send to function.
Right, it really depends on where you want to display the information from the $current_page. If you want to include information from the $current_page post in the notification, then you should create generic fields in the Form to store that information, and create custom placeholders to access those generic fields and include those placeholders in the notification text. Unfortunately the Form does not retain the "context" of the current page automatically for notifications, so you must add the information you want to access in generic fields.
I can help you set up placeholders using our API if you need assistance, just let me know.
Okay, currently I am trying to add the post authors name and the post title to the notification. Can you give me an example of what I would need to add in the generic hidden field in terms of "Field slug" and "Default field value", as well as what to add to the notification body?
Sure, I'm using the documentation here as a guide:
https://toolset.com/documentation/programmer-reference/forms/how-to-use-custom-placeholders-in-cred-notifications/
For the post title:
[cred_generic_field field='current-post-title' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-title item='$current_page']"
}
[/cred_generic_field]
For the post author name:
[cred_generic_field field='current-post-author' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-author item='$current_page']"
}
[/cred_generic_field]
The relevant PHP code with some conditional logic built in to prevent errors if those generic fields are undefined for any reason:
/**
* Toolset support reference: https://toolset.com/forums/topic/contact-the-post-author/
* Add %%CURRENT_POST_TITLE%% AND %%CURRENT_POST_AUTHOR%% placeholders for use in CRED notifications
*/
add_filter('cred_subject_notification_codes', 'custom_generic_field_notification', 10, 1);
add_filter('cred_body_notification_codes', 'custom_generic_field_notification', 10, 1);
function custom_generic_field_notification( $defaultPlaceHolders ) {
if( !isset( $_REQUEST['current-post-title'] ) || !isset( $_REQUEST['current-post-author'] ) ) {
return $defaultPlaceHolders;
}
$newPlaceHolders = array(
'%%CURRENT_POST_TITLE%%' => $_REQUEST['current-post-title'],
'%%CURRENT_POST_AUTHOR%%' => $_REQUEST['current-post-author']
);
return array_merge($defaultPlaceHolders, $newPlaceHolders );
}
Then in the notification you should have the ability to display the current post title and current post author information with the placeholders %%CURRENT_POST_TITLE%% and %%CURRENT_POST_AUTHOR%%. Please give this a try and let me know the results.
Thanks, I think this has worked, I'm just testing a few more times before deploying it on the live site so if its okay, can I leave the ticket open till then?
It was really useful to have the contextual examples in your last reply, it made the instructions on the page you linked to make a lot more sense to me.
Sure, I'll stand by for your update. Looks like there were typos in the field slugs in the PHP script, I'm adjusting those in the code snippet above. There were extra "r"s in all the "currrent"s, like this:
$_REQUEST['currrent-post-title']
Those should be:
$_REQUEST['current-post-title']
My issue is resolved now. Thank you!