Home / Archive / MemberPress: Custom Course Overview Shortcode for Better Preview
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Custom Course Overview Shortcode for Better Preview

This code registers a new shortcode: [mepr-mpcs-course-overview]. Adding this shortcode will display the overview of a course using the MemberPress Classic template style.

The shortcode accepts the following parameters (only ONE parameter should be used):

course_id – The ID of the MemberPress course to display;
lesson_id – The ID of the MemberPress lesson from the course to display;
quiz_id – The ID of the MemberPress quiz from the course to display.

Thus, for example, to show the overview of the MemberPress course with the ID of 123, the shortcode used would look like this:

[mepr-mpcs-course-overview course_id="123"]

Code Preview
php
<?php
use memberpress\courses\models as models;
// Register classroom Styles
function wpdocs_register_plugin_styles() {
	wp_enqueue_style( 'mpcs-fontello-styles', plugins_url( 'memberpress-courses/public/fonts/fontello/css/mp-courses.css' ) );
	wp_enqueue_style( 'mpcs-progress', plugins_url( 'memberpress-courses/public/css/progress.css' ) );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_register_plugin_styles' );
// The "[mepr-mpcs-course-overview]" shortcode
add_shortcode( 'mepr-mpcs-course-overview', function( $attributes ) {
    $content = '';
    if( isset( $attributes['course_id'] ) && is_numeric( $attributes['course_id'] ) ) {
      $course = new models\Course( $attributes['course_id'] );
    }
    elseif( isset( $attributes['section_id'] ) && is_numeric( $attributes['section_id'] ) ) {
      $section = new models\Section( $attributes['section_id'] );
      $course = $section->course();
    }
    elseif( isset( $attributes['lesson_id'] ) && is_numeric( $attributes['lesson_id'] ) ) {
      $lesson = new models\Lesson( $attributes['lesson_id'] );
      $course = $lesson->course();
    }
    elseif( isset( $attributes['quiz_id'] ) && is_numeric( $attributes['quiz_id'] ) ) {
      $quiz = new models\Quiz( $attributes['quiz_id'] );
      $course = $quiz->course();
    }
	
    $current_user_id = 0;
    if( is_user_logged_in() ) {
      $current_user_id = get_current_user_id();
    }
    if( isset( $course ) && $course !== false ) {
      \ob_start();
        ?>
          <div class="mpcs-course-overview">
        <?php
        if( isset( $attributes['hide_title'] ) && ( $attributes['hide_title'] == 'true' || $attributes['hide_title'] == 1 ) ):
        else:
          ?>
            <h2 class="mpcs-course-title"><?php echo $course->post_title; ?></h2>
          <?php
        endif;
		
	  $sections = $course->sections();
		   require(\MeprView::file('/courses/courses_section_lesson_list'));
        ?>
          </div>
        <?php
      $content = \ob_get_clean();
    }
    return $content;
} );

Comments

Add a Comment