最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Claim Listing functionality - how to send email to users when their claim has been approved or denied

programmeradmin1浏览0评论

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();
            }
        }
    }
}

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();
            }
        }
    }
}
Share Improve this question edited Sep 3, 2019 at 12:02 jand1 asked Sep 1, 2019 at 16:18 jand1jand1 314 bronze badges 5
  • 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 Commented Sep 1, 2019 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 Commented Sep 2, 2019 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 Commented Sep 3, 2019 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 Grossherr Commented Sep 3, 2019 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 Commented Sep 7, 2019 at 8:07
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论