[CoursePress] Include course enrollment date in course certificate

Would it be possible to include the course enrollment date in the course completion certificate?

I know that this is a custom development request as currently, the only macros available to use are:
COURSE_NAME, COURSE_SUB_TITLE, COURSE_OVERVIEW, COURSE_UNIT_LIST, DOWNLOAD_CERTIFICATE_LINK, DOWNLOAD_CERTIFICATE_BUTTON, STUDENT_WORKBOOK

But if someone knows how this could be achieved, we'd really appreciate the help. Thanks!

  • Panos

    Hi carl ,

    In order to add the enrollment date macro in the certificate, you can add the following snippet in a mu-plugin:

    function wpmudev_cp_custom_certificate_vars( $vars ) {
    
        if ( ! isset( $_REQUEST['c'] ) || ! isset( $_REQUEST['u'] ) ) {
            return $vars;
        }
    
        $course_id                  = (int) $_REQUEST['c'];
        $student_id                 = (int) $_REQUEST['u'];
        $date_format                = get_option( 'date_format', 'Y-m-d' ) . ' ' . get_option( 'time_format', 'H:i:s' );
        $enrollment_date            = date(
                                        $date_format,
                                        strtotime( CoursePress_Data_Course::student_enrolled( $student_id, $course_id ) )
                                    );
    
        $vars['ENROLLMENT_DATE']    = $enrollment_date;
    
        return $vars;
    }
    
    add_filter( 'coursepress_basic_certificate_vars', 'wpmudev_cp_custom_certificate_vars', 20 );

    and use the ENROLLMENT_DATE macro in the certificate content.

    As in your question you included some macro's from the Completion Page, I'm including a snippet that you can use to add the same macro there too:

    add_filter( 'coursepress_virtual_page', function( $vp_args, $cp ){
    
        if (
            ! isset( $vp_args['ID'] ) ||
            ! isset( $vp_args['content'] ) )
        {
            return $vp_args;
        }
    
        $course_id = (int) $vp_args['ID'];
        $student_id = isset( $cp->student_id ) ? (int) $cp->student_id : ( is_user_logged_in() ? get_current_user_id() : false );
    
        if ( ! $student_id ) {
            return $vp_args;
        }
    
        $date_format = get_option( 'date_format', 'Y-m-d' ) . ' ' . get_option( 'time_format', 'H:i:s' );
    
        $enrollment_date = isset( $cp->is_enrolled ) ? date( $date_format, strtotime( $cp->is_enrolled ) ) : false;
    
        if ( ! $enrollment_date ) {
            $enrollment_date = date(
                                    $date_format,
                                    strtotime( CoursePress_Data_Course::student_enrolled( $student_id, $course_id ) )
                                );
        }
    
        $replacements = array(
            'ENROLLMENT_DATE' => $enrollment_date
        );
    
        $vp_args['content'] = str_replace( array_keys( $replacements ), array_values( $replacements ), $vp_args['content'] );
    
        return $vp_args;
    
    }, 20 ,2 );

    In case you are not familiar with mu-plugins, you can read about them here:
    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.