Aleš Sýkora / February 29, 2024 / 0 comments

LearnDash: Custom Shortcodes for Kadence Query Loop

Post summary: I just tried to use Shortcodes from LearnDash in Kadence Theme query loop without andy luck to make them work. So I made mine own ones.

However, it doesn’t work out of the box currently, because they can’t get the current post id in the Kadence ADV Query block.

How to use them then? I suggest to use plugin called Meta Field Block – Plugin WordPressu | WordPress.org Česko. You can add the shortcodes into the query with this block and it does some magic tricks, that enable the id’s of posts in query and shortcodes works then.

To add the shortcodes below, use this settings of Meta Field Block.

  • Field type = Dynamic field
  • FIELD NAME = Shortcode.

[ShortCode] Get LearnDash Course Price in the loop

This shortcode will return Price of course or Free or Bought status of the course in the loop. Use [learndash_course_price_custom]. Don’t forget to change the currency to your needs. What it does in detail?

  1. Retrieve the Current Course ID: It first gets the ID of the current course within the loop.
  2. Check Course Validity: It checks if the current post is actually a course by comparing its post type with 'sfwd-courses'. If the post is not a course, it returns an empty string, effectively displaying nothing.
  3. Retrieve User’s Course Progress: It fetches the course progress for the currently logged-in user by accessing user meta data stored under _sfwd-course_progress.
  4. Check User’s Course Progress: It checks if the user has made any progress in the course. If the user has progress (which implies the course has been purchased), it displays "Purchased".
  5. Retrieve Course Price Information: It retrieves detailed information about the course price from the course metadata. This includes the type of pricing (free, etc.) and the actual price.
  6. Display Logic for Price: If the course is marked as free or if no price is set, it returns "Free". Otherwise, it displays the course price followed by the "Kč" currency symbol.
LearnDash custom shortcode for display the course price
LearnDash custom shortcode for display the course price
function custom_learndash_course_price_shortcode( $atts ) {
    // Retrieve the ID of the current course in the loop
    $course_id = get_the_ID();
    
    // Check if the current post is a course
    if ( 'sfwd-courses' != get_post_type( $course_id ) ) {
        return ''; // If the current post is not a course, return an empty string
    }

    $user_id = get_current_user_id();
    // Retrieve course progress for the user
    $course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true );

    // Check if the user has any progress in the course
    if ( !empty( $course_progress ) && isset( $course_progress[$course_id] ) && !empty( $course_progress[$course_id] ) ) {
        return 'Purchased'; // If the user has the course, display "Purchased"
    }

    // Retrieve information about the course price
    $course_meta = get_post_meta( $course_id, '_sfwd-courses', true );
    $price_type = isset( $course_meta['sfwd-courses_course_price_type'] ) ? $course_meta['sfwd-courses_course_price_type'] : '';
    $price = isset( $course_meta['sfwd-courses_course_price'] ) ? $course_meta['sfwd-courses_course_price'] : '';

    // If the course is free or no price is set, return "Free"
    if ( 'free' == $price_type || empty( $price ) ) {
        return 'Free';
    }

    // Display the course price with "Kč" currency after the price
    return $price . ' Kč';
}
add_shortcode( 'learndash_course_price_custom', 'custom_learndash_course_price_shortcode' );

[ShortCode] Display the LearnDash progress bar if user own the course

This shortcode will display the progress in the course, if user started it (free course) or bought it. Use [learndash_course_progress_custom] shortcode which dynamically retrieves the current course’s ID in a loop and checks if the post is a course. If the post is indeed a course and the user has made any progress in it, it displays the course progress using LearnDash’s standard function learndash_course_progress. If the user has no progress, nothing is displayed. This is useful for showing personalized course progress information directly within course-related content.

LearnDash custom shortcode conditional display progress bar in a Kadence Loop Card
LearnDash custom shortcode conditional display progress bar in a Kadence Loop Card
function custom_learndash_course_progress_shortcode( $atts ) {
    // Retrieve the current course ID in the loop
    $course_id = get_the_ID();
    
    // Check if the current post is a course
    if ( 'sfwd-courses' != get_post_type( $course_id ) ) {
        return ''; // If the current post is not a course, return an empty string
    }

    $user_id = get_current_user_id();
    // Retrieve the course progress for the user
    $course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true );

    // Check if the user has any progress in the course
    if ( empty( $course_progress ) || !isset( $course_progress[$course_id] ) || empty( $course_progress[$course_id] ) ) {
        return ''; // If the user has no progress in the course, display nothing
    }

    // Use the standard LearnDash function to display course progress
    $progress_output = learndash_course_progress( array(
        'user_id'   => $user_id,
        'course_id' => $course_id,
        'echo'      => false, // Do not echo the output, but return it as a string
    ) );

    return $progress_output;
}
add_shortcode( 'learndash_course_progress_custom', 'custom_learndash_course_progress_shortcode' );

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts