3

We have a claim listing plugin, it allows items(posts) to be claimed by users. It requires the user to "register" in order to claim that item therefore they have to enter a username and their email.

After they have submitted a claim, an email will be sent to the admin email(us) that a user is claiming for the item and gives us the option to approve or deny it.

When we approve or deny it, unfortunately no email is sent to the user informing them that the item has been approved or denied.

How could we get wordpress to send an email to the user informing them of our response?

Code: CodeShare (the code is too long and is breaking when I paste it here guys, sorry!) As you can see, there is a wp_mail functionality in there but that's for when an email is sent to the admin that a request of an item ownership has been requested)

This seems to be function with the issue(code below), what do we then need to add to send an email to the user to say that their request has been approved?

public static function claimListingActions(){
    if(isset($_GET['post_type']) && $_GET['post_type'] === 'ait-item'){

        if (isset($_GET['claim-action']) && !empty($_GET['post-id'])) {
            $postID = intval($_GET['post-id']);
            // admin can approve all ratings
            if (current_user_can('manage_options')) {
                switch($_GET['claim-action']){
                    case 'approve':
                        $redirect = admin_url('edit.php?post_type=ait-item&ait-notice=claim-approved');

                        $data = get_post_meta($postID, 'ait-claim-listing', true);
                        $data['status'] = 'approved';

                        update_post_meta($postID, 'ait-claim-listing', $data);

                        $user = get_user_by('email', $data['owner']);

                        // update also the _ait-item_item-author data field -> prevent errors
                        update_post_meta($postID, '_ait-item_item-author', array('author' => $user->ID));

                        wp_update_post( array('ID' => $postID, 'post_author' => $user->ID), true );
                    break;
                    case 'decline':
                        $redirect = admin_url('edit.php?post_type=ait-item&ait-notice=claim-declined');

                        $data = get_post_meta($postID, 'ait-claim-listing', true);
                        $data['status'] = 'unclaimed';
                        $data['owner'] = '-';
                        $data['date'] = '-';

                        update_post_meta($postID, 'ait-claim-listing', $data);

                        $user = new WP_User($data['author']);

                        // update also the _ait-item_item-author data field -> prevent errors
                        update_post_meta($postID, '_ait-item_item-author', array('author' => $user->ID));

                        wp_update_post( array('ID' => $postID, 'post_author' => $user->ID) );
                    break;
                }
                wp_safe_redirect( $redirect );
                exit();
            }
        }
    }
}
  • Hi, welcome to WordPress SE. Normally, the community flags third-party plugins as off-topic. However, I think we can get past that if you post just the part of the code that is giving you the trouble. – Matthew Brown aka Lord Matt Sep 1 at 23:47
  • 1
    thank you! I have added the code that seems to be the function I understand that triggers when we approve an item. – jand1 Sep 2 at 16:47
  • Unfortunately there isn't anything obvious in that code you can hook to act on the change of status. You can hook update_ait-item_meta which would be called by update_post_meta but only with the new data value: you'd have to get the old value and compare to be sure that the status had changed I'd think. It's going to be simplest to add a hook to this code I think that you can implement and send the notification emails. You'd then need to be careful about updates losing your change, but you also ought to contact the plugin author and ask them to add a hook here too and use that in the future. – Rup Sep 3 at 12:49
  • I couldn't see any mail sending functionality at a glance, your question makes it seem like it should be there. But either way, why don't you just add wp_mail() functionality to the code posted in your question. If I'm not mistaken, then all you need to know is there, so is it case approve or decline, and the user data. As far as I can tell, that's all you need to send a your claim has been approved/declined mail to the user. – Nicolai Sep 3 at 13:58
  • @Nicolai yes, that's essentially what I want but dont know how and where to write it. If you look at my entire code in the Codeshare link I put in the original question, around line 492-536 is the code for the notification for the Admin. This code is basically saying send an email when a requst to claim the item has been sent/requested. The the admin will receive an email. Would you be able to show me how to do it for the functionality in question? – jand1 Sep 7 at 8:07
0

Unfortunately, the developers didn't give you any hooks in their code to allow for customization without hacking it. But all is not lost - there are some hooks in WP that you could use to trigger your emails.

The ideal would be to trigger off the update_post_meta(). That function is simply a wrapper for update_metadata() which has lots of hooks in it.

Here's an untested example of a direction you could go. Hopefully it gives you something to work off of.

/**
 * @param int    $meta_id    ID of updated metadata entry.
 * @param int    $object_id  Post ID.
 * @param string $meta_key   Meta key.
 * @param mixed  $meta_value Meta value. This will be a PHP-serialized string representation of the value if
 *                           the value is an array, an object, or itself a PHP-serialized string.
 */
add_action( 'updated_postmeta', function( $meta_id, $object_id, $meta_key, $meta_value ) {
    if ( 'ait-claim-listing' == $meta_key ) {
        if ( 'approved' == $meta_value['status'] ) {
            $email_to = $meta_value['owner'];
            $subject = 'Your approved email subject';
            $message = 'Your approved email message...';
        }

        if ( 'unclaimed' == $meta_value['status'] ) {
            $email_to = $meta_value['author'];
            $subject = 'Your unclaimed email subject';
            $message = 'Your unclaimed email message...';
        }

        $result = wp_mail( $email_to, $subject, $message );
    }
}, 10, 4 );

Keep in mind, this is untested and just a thought process based on the information in your question. You may need to make some adjustments to make it really work.

  • Hi, thank you very much for your reply. I will test this now. There isnt any problem with hacking it, in fact they allow for customisation to be done as I've done it already with a few of their plugin functionality. But I'm still a newbie and don't now how to work this one out. I'll give my feedback later after testing. – jand1 Sep 7 at 8:03
  • Hacking plugin files directly should only ever be a last resort. Even then, it's a bad idea for a number of reasons, not the least of which is the fact that any changes you make will be overwritten when the plugin is updated. This is what filter and actions hooks are for - so that you don't have to hack a file you shouldn't be modifying. – butlerblog Sep 7 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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