2

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?

5
  • 1
    What is "wp_user_frontend" ? If this is plugin please link to it so others don't have to look it up.
    – Rarst
    Commented Sep 14, 2013 at 8:26
  • 1
    For a complete context, how are you sending the approval email?
    – brasofilo
    Commented Sep 14, 2013 at 9:58
  • Here is the snipplet which send the approval mail: paulund.co.uk/send-email-to-wordpress-author-on-post-publish But this is automated. I need the rejection to be per click
    – Asped
    Commented Sep 14, 2013 at 10:20
  • No time for a complete answer, so I comment to give you a hint what to do. You can either use ajax or put the button in a form to execute your reject function on button click. Take a look around, I'm 100% certain this has been answered on here. Good luck! Commented Sep 14, 2013 at 13:09
  • Another idea, use the wp_trash_post or the before_delete_post action to hook in. There are others - documented here: codex.wordpress.org/Plugin_API/Action_Reference Commented Sep 14, 2013 at 13:21

1 Answer 1

3

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

6
  • Wow, thanks, I will try this and let you know if it worked
    – Asped
    Commented Sep 16, 2013 at 8:08
  • Thanks! The code works perfectly, I just needed to remove the anonymous functions, because there is an older PHP version on the server.
    – Asped
    Commented Sep 16, 2013 at 9:52
  • @Asped glad it works. I've moved all the code in a plugin, I've edited the answer including plugin link. If you prefer you can use that plugin. It use OOP approach, and doesn't make use of anonymous functions, so it's compatible with older version of PHP.
    – gmazzap
    Commented Sep 16, 2013 at 11:28
  • Thanks, I will give it a try. One additional thing came to my mind here - I would need to set the status to "edited" (id an edited post has been accepted) or "rejected" after the post been rejected. Becasue this way the admin does not know if he has already sent the rejection mail - he does not see it anywhere
    – Asped
    Commented Sep 16, 2013 at 16:37
  • @Asped I'd not use a custom post status for that, because if so you have to write a lot of code to make it works without any issue. For me it's better use an hidden meta field and working on manage_posts_columns and manage_post_posts_custom_column hooks show on the post lists if a notify was already sended or not. A metabox can be used to show same thing in post edit page. The field can be auto-cleaned using post status transitions hooks. This is a 'to-do' for my plugin, but you can implement these features now, and if you want my plugin on github is open to pull requests..
    – gmazzap
    Commented Sep 16, 2013 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.