Created
August 24, 2026 15:17
-
-
Save dwanjuki/3cdbcdc54a556189d6b81fffd65f582c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Add the course featured image thumbnail to the [pmpro_my_courses] shortcode output. | |
| * | |
| * 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 my_pmpro_courses_add_featured_image_to_my_courses( $html, $courses ) { | |
| if ( empty( $courses ) ) { | |
| return $html; | |
| } | |
| foreach ( $courses as $course ) { | |
| if ( ! has_post_thumbnail( $course->ID ) ) { | |
| continue; | |
| } | |
| $thumbnail = get_the_post_thumbnail( $course->ID, 'thumbnail', array( 'class' => 'pmpro_courses-list-item-thumbnail' ) ); | |
| // Insert the thumbnail right after the opening <a> tag for this course's list item. | |
| $pattern = '/(<li id="pmpro_courses-course-' . intval( $course->ID ) . '"[^>]*>\s*<a[^>]*>)/'; | |
| $html = preg_replace( $pattern, '$1' . $thumbnail, $html, 1 ); | |
| } | |
| return $html; | |
| } | |
| add_filter( 'pmpro_courses_get_courses_html', 'my_pmpro_courses_add_featured_image_to_my_courses', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment