Browse: Home / Hooks /

learndash_courseinfo

apply_filters( 'learndash_courseinfo',  string $shortcode_output,  array $shortcode_attributes )

Filters courseinfo shortcode output.


Description #


Parameters #

$shortcode_output

(string) The output of courseinfo shortcode.

$shortcode_attributes

(array) An array of shortcode attributes.


Source #

File: includes/shortcodes/ld_courseinfo.php


Examples #

Note: Extended code example below not guaranteed, you may need to consult with a developer

 <?php
/**
 * Example usage for learndash_courseinfo filter.
 */
add_filter(
	'learndash_courseinfo',
	function( $content = '', $atts = array() ) {
		if ( ( empty( $content ) ) && ( is_array( $atts ) ) && ( ! empty( $atts ) ) ) {
			if ( ( isset( $atts['show'] ) ) && ( ! empty( $atts['show'] ) ) ) {

				// First determine the course_id and user_id logic
				$course_id = ! empty( $course_id ) ? $course_id : @$_GET['course_id'];
				$user_id   = ! empty( $user_id ) ? $user_id : @$_GET['user_id'];
				if ( empty( $user_id ) ) {
					$user_id = get_current_user_id();
					/**
					 * Added logic to allow admin and group_leader to view certificate from other users.
					 *
					 * @since 2.3.0
					 */
					$post_type = '';
					if ( get_query_var( 'post_type' ) ) {
						$post_type = get_query_var( 'post_type' );
					}

					if ( $post_type == 'sfwd-certificates' ) {
						if ( ( ( learndash_is_admin_user() ) || ( learndash_is_group_leader_user() ) ) && ( ( isset( $_GET['user'] ) ) && ( ! empty( $_GET['user'] ) ) ) ) {
							$user_id = intval( $_GET['user'] );
						}
					}
				}

				switch ( $atts['show'] ) {
					case 'course_price':
						if ( ! empty( $course_id ) ) {
							$course_price = learndash_get_setting( $course_id, 'course_price' );
							if ( isset( $shortcode_atts['decimals'] ) ) {
								$shortcode_atts['decimals'] = 2;
							}
							$course_price = number_format( floatval( $course_price ), $shortcode_atts['decimals'], '.', '' );
							$content     .= $course_price;
						}
						break;

					case 'course_points':
						if ( ! empty( $course_id ) ) {
							$course_points = learndash_get_course_points( $course_id );
							$course_points = intval( $course_points );
							if ( ! empty( $course_points ) ) {
								$content .= $course_points;
							}
						}
						break;

					case 'user_course_points':
						if ( ! empty( $user_id ) ) {
							$user_course_points = learndash_get_user_course_points( $user_id );
							$content           .= $user_course_points;
						}
						break;

					default:
						break;
				}
			}
		}

		// Always return $content
		return $content;

	},
	10,
	2
);