I am searching for a solution for the following problem:
I am using WP User Frontend, so that users can create posts in the frontend. After saving, the admin needs to review the post, and the publish (if approved), or reject (if not approved).
If the post is approved, an email is automatically sent to the user, that his post has been approved (this I already found out how to do).
But if the admin does not approve, how to send the user an email, with a possible custom text (why he did not approve) from the backend, per click on a button?
In the post edit page, just under the ‘Publish’ button, add a button, ‘Reject and Notify’ that once clicked open a modal (thickbox) window that contain a form (pre-compiled with the post author name, post title and a standard message) that you can use to send the email to post author.
To accomplish this you need:
'post_submitbox_misc_actions'
action hook to print it in the right placeThe 2 last functions can work easily with Ajax Api, so you need also a js file where put the ajax and other js code.
So, another function is needed for the script enqueueing and script localize with
wp_localize_script
The button should contain also js-accessible information about current post, a
data-*
attribute can be used for that.For both the functions that print the button and the form (points 1. and 2.) you have to check that:
In the function that sends the email (points 3.) you have to do some security tasks:
After that, this function should output some info on the mail send process (message sended or not) and output them as json, in this way via js it’s possible to give a feedback on the modal.
If an error occurred, is a good idea also output some debug info, maybe checking if debugging is active or not in WP.
Edit: Plugin Code
In the original answer I posted the code here. Now I’ve just created a plugin to include all the workflow described here using OOP approach. The plugin is ready for localization and already localized in italian.
The plugin in GPL licensed and available on GitHub.
Plugin Screenshots
If something goes wrong and debug is active…