我想在更改自定义分类时向用户发送电子邮件

时间:2022-01-10 作者:Talha Ahmad

大家好,我是wordpress开发的新手。我正在开发一个插件,在该插件中,我创建了一个带有应用程序名称的自定义帖子类型,当用户从前端提交表单时,将使用用户数据创建一个新的应用程序。我有一个自定义分类法,名称为application\\u status。当管理员将分类法的状态从挂起更改为接受时,我想向用户发送一封电子邮件。我有一个大致的想法如何做,就像我可以得到新的更新值,并将其与以前的值(保存在数据库中)进行比较,如果它被更改,我可以发送电子邮件。那么,有谁能指导我如何在存储新更改的分类法值之前获取它,以便将其与以前的值进行比较呢。

从一些参考资料中我看到了钩子

       $this->loader->add_filter( \'transition_post_status\', $plugin_admin, \'send_mail_when_status_changed\', 10, 3);
功能是

function send_mail_when_status_changed($new_status, $old_status, $post ) {
        if ( \'publish\' !== $new_status ||
            $new_status === $old_status ||
            \'application\' !== get_post_type( $post ) ) {
            return;
        }
    
        // Get the post author data.
        if ( ! $user = get_userdata( $post->post_author ) ) {
            return;
        }
        //check if the taxonomy is changed
        $appstatus=\'Pending\';
        $terms = wp_get_object_terms( $post->ID, \'application_status\');
                    foreach ( $terms as $term ) {
                $appstatus=$term->name; 
        } 
        //donot know how to get compare the value with the changed value

    
        // Compose the email message.
        // $body = sprintf( \'Hey %s, your awesome post has been published! See ,
        //  esc_html( $user->display_name ),
        //  get_permalink( $post )
        // );
    
        // // Now send to the post author.
        // wp_mail( $user->user_email, \'Your post published!\', $body );
    }

2 个回复
最合适的回答,由SO网友:Talha Ahmad 整理而成

使用挂钩解决了问题

add_filter( \'wp_insert_post_data\', \'send_mail_when_status_changed\', 10, 3);
它向函数传递了三个参数$data,$postarr,$unsanitized_postarr 使用它们,我得到了更新后的分类法id,并将其与数据库中保存的分类法id进行了比较,然后根据id进行操作。有关更多信息,请访问此处https://developer.wordpress.org/reference/hooks/wp_insert_post_data/

下面是函数

 function send_mail_when_status_changed($data, $postarr, $unsanitized_postarr   ){
        if($data[\'post_type\']!=\'application\'){ //checking if the post type is application
            return $data;
        }
        $post_ID=!empty($postarr[\'post_id\']) ? $postarr[\'post_id\'] :\'\';
        //getting the updated taxonomy ID
        $updated_status_ID=!empty($postarr[\'tax_input\'][\'application_status\'][1]) ? $postarr[\'tax_input\'][\'application_status\'][1] : \'\';//here
        //getting the name of the updated status
        $updated_status_term=get_term($updated_status_ID );
        $updated_status_name=!empty($updated_status_term->name) ?$updated_status_term->name: \'\'; //here
        $updated_status_name;
        $updated_status_name=strtolower($updated_status_name);
        //getting the id of the post author
        $post_user_id=$data[\'post_author\'];
        //getting the email of the post author
        $user_email = get_the_author_meta( \'user_email\',$post_user_id);
        //fethcing the previous taxonomy status ID from the database
        $terms = wp_get_object_terms( $post_ID, \'application_status\');
        $old_status_ID=\'\';
        foreach ( $terms as $term ) {
            $old_status_ID=$term->term_id; 
        } 
        if($old_status_ID!=$updated_status_ID){
            if($updated_status_name==\'accepted\'){
                // Email subject"
                $subject = \'Your Application Status\';
            
                // Email body
                $message = \'Your Application for the Job was accepted \';
            
                wp_mail( $user_email, $subject, $message );
            }else if($updated_status_name==\'rejected\'){
                // Email subject, "New {post_type_label}"
                $subject = \'Your Application Status\';
            
                // Email body
                $message = \'Your Application for the Job was Rejected \';
            
                wp_mail( $user_email, $subject, $message );
            }else if($updated_status_name==\'pending\'){
                // Email subject, "New {post_type_label}"
                $subject = \'Your Application Status\';
            
                // Email body
                $message = \'Your Application for the Job was Pending \';
            
                wp_mail( $user_email, $subject, $message );
            }
        }
        return $data;
    } 

SO网友:DeltaG

您可以使用pre_post_update 钩子以在保存帖子之前获取新值。通过这种方式,您可以检查值是否已更改(此时旧值仍在数据库中),并发挥您的魔力。

看见https://developer.wordpress.org/reference/hooks/pre_post_update/

相关推荐