Send email to user that his post has been rejected

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?

1 Answer
1

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:

  1. A function that print the button. You can use the 'post_submitbox_misc_actions' action hook to print it in the right place
  2. A function that output the form in a modal window. This function should use the current post id to get the author and put the author email in a hidden field.
  3. A function that send the email when the form is submitted

The 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:

  • the current user has editor capabilities
  • the current page is the post edit page (post.php)

In the function that sends the email (points 3.) you have to do some security tasks:

  • check if the current user has editor capabilities
  • check a nonce (previously added as hidden field on the form)

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


The Reject and Notify button


The modal form


Success Message


If something goes wrong and debug is active…

Error message with debug

Leave a Comment