Send an email copy when certificate email is sent

I want to send an email to the admins whenever a certificate is issued, is there an action in the plugin that can be used to achieve that ?

  • Adam Czajczyk

    Hello morfjord

    I hope you're well today!

    If you want to send a notification about the generated certificate you can hook to

    coursepress_pre_insert_post

    filter. It take's "$post" variable as a parameter so I believe you could do something like that

    add_filter(  'coursepress_pre_insert_post',  'my_cert_mail', 10, 1 );
    function my_cert_mail( $post ) {
    
    if ( $post['post_type'] == 'cp_certificate' ) {
    
    // $post contains basic information about certificate
    // send your notification here
    
    }
    
    return $post;
    }

    $post contains:

    - ID (post id)
    - post_author (which is a student ID)
    - post_parent (which is a course ID)
    - post_content (which contains certificate data so you can "var_dump" that to see what could be useful for you).

    Kind regards,
    Adam

  • Konstantinos Xenos

    Hi morfjord !

    I just wanted to add a bit more information on Adam Czajczyk correct reply just as a more 'complete' solution.

    This hook is using Adam's suggestion, it is running whenever a cert is issued and the corresponding post is saved in the database. You can change the $to variable to the email that you want to receive the notification at.

    add_filter( 'coursepress_pre_insert_post', 'my_cert_mail', 10, 1 );
    function my_cert_mail( $post ) {
    
    	if ( 'cp_certificate' === $post['post_type'] ) {
    		$headers = array( 'Content-Type: text/html; charset=UTF-8' );
    		$body    = $post['post_content'];
    		$subject = 'Certificate Issued';
    
    		$to      = 'admin@yoursite.com';
    
    		wp_mail( $to, $subject, $body, $headers );
    	}
    
    	return $post;
    }

    While this hook will run whenever there's an e-mail sent out to a student to get their certificate. Again you should change the $to var to whatever email you want the notification to be send at. You can also customize the $body and $subject a bit with this that's why I'm showing this way of hooking into the certs as well.

    add_action( 'coursepress_email_pre_send', 'send_custom_cp_mail_to_admins', 15, 2 );
    function send_custom_cp_mail_to_admins( $args, $type ) {
    
    	if ( 'basic_certificate' === $type ) {
    		$student_name      = ( ! empty( $args['first_name'] ) ) ? $args['first_name'] : '';
    		$student_last_name = ( ! empty( $args['last_name'] ) ) ? $args['last_name'] : '';
    		$course_name       = ( ! empty( $args['course_name'] ) ) ? $args['course_name'] : '';
    		$headers           = array( 'Content-Type: text/html; charset=UTF-8' );
    
    		$to      = 'admin@yoursite.com';
    		$subject = 'Certificate Issued for student: ' . $student_name . ' ' . $student_last_name;
    		$body    = "Student {$student_name} {$student_last_name} has just received a certificate for {$course_name}.";
    
    		wp_mail( $to, $subject, $body, $headers );
    	}
    }

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.