Skip to content

Create shortcode to show all PMPro courses with lesson count #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions show-all-courses-with-lesson-count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* This recipe builds a new shortcode for all pmpro courses list and shows the lesson count for each course in a table layout
* Use the shortcode [pmpro_courses_with_lessons] to display your course with lessons on your site

* title: show all courses with lesson count
* layout: snippet
* collection: add-ons
* category: pmpro-courses
* link: TBD
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function pmpro_courses_with_lessons_init() {
// Check if PMPro Courses is active
if ( ! function_exists( 'pmpro_courses_get_courses' ) ) {
return;
}

// Add the shortcode
function pmpro_courses_with_lessons_shortcode( $atts ) {
$courses = pmpro_courses_get_courses();

if ( empty( $courses ) ) {
return '<p>No courses available.</p>';
}

// Output styles inline to keep everything in one file
ob_start();
?>
<style>
.pmpro-courses-table {
border-top: 1px solid #ddd;
margin-top: 1em;
max-width: 600px;
}

.pmpro-courses-header {
font-weight: bold;
padding: 0.5em 0;
border-bottom: 1px solid #ddd;
text-transform: uppercase;
}

.pmpro-course-row {
display: flex;
justify-content: space-between;
padding: 0.5em 0;
border-bottom: 1px solid #eee;
}

.pmpro-course-title a {
text-decoration: none;
color: #333;
}

.pmpro-course-lessons {
font-weight: bold;
}
</style>
<?php

echo '<div class="pmpro-courses-table">';
echo '<div class="pmpro-courses-header"><strong>KURSER</strong></div>';

foreach ( $courses as $course ) {
$lesson_count = pmpro_courses_get_lesson_count( $course->ID );

echo '<div class="pmpro-course-row">';
echo '<div class="pmpro-course-title"><a href="' . esc_url( get_permalink( $course->ID ) ) . '">' . esc_html( get_the_title( $course->ID ) ) . '</a></div>';
echo '<div class="pmpro-course-lessons">' . esc_html( $lesson_count ) . ' lesson' . ( $lesson_count !== 1 ? 's' : '' ) . '</div>';
echo '</div>';
}

echo '</div>';

return ob_get_clean();
}

add_shortcode( 'pmpro_courses_with_lessons', 'pmpro_courses_with_lessons_shortcode' );
}
add_action( 'plugins_loaded', 'pmpro_courses_with_lessons_init' );