Snippets

LearnDash Override the LearnDash core reporting column headers

Created by Paul Menard last modified
/**
 * Override the LearnDash core reporting column headers. 
 *
 * @param $data_headers array of headers. See notes below for exact structure
 * @param $data_slug stirng for the type of report 'user-courses' or 'user-quizzes'
 *
 * @return $data_headers array 
 *
 * The follow is an example of the data structure used for the headers. Note this is NOT
 * a simple key/value array. 
 * $data_headers['user_id']  = 	array( 
 *										'label'		=>	'user_id',
 *										'default'	=>	'',
 *										'display'	=>	array( $this, 'report_header_user_id' )
 *									);
 *
 * 'label' This is used in place of the array item key for the column header value.
 * 'default' This is the default value of the field. For example empty ''. Or can be 0 or some specific string value.
 * 'display' This should be a callback to your custom function to handle the value determination. 
*/
add_filter( 'learndash_data_reports_headers', function( $data_headers, $data_slug ) { 

	if ( $data_slug == 'user-courses' ) {

		// Example on how to change the output column header label used. 
		//if ( isset( $data_headers['customer_id'] ) ) {
		//	$data_headers['customer_id']['label'] = 'Customer ID';
		//}
		//if ( isset( $data_headers['course_title'] ) ) {
		//	$data_headers['course_title']['label'] = 'Course Title';
		//}


		// As an example assume you want to add two new custom columns called 'customer_id' and 'payroll_id. These fields are already stored as part of the WP user meta. 
		// Below note on the 'display' parameter we are assigning the same callback function. This is NOT a requirement. You can assigne different callback functions as needed.  
		if ( !isset( $data_headers['customer_id'] ) ) {
			$data_headers['customer_id'] = array(
				'label'		=>	'customer_id',
				'default'	=>	'',
				'display'	=>	'ld_custom_reporting_column'
			);
		}

		if ( !isset( $data_headers['payroll_id'] ) ) {
			$data_headers['payroll_id'] = array(
				'label'		=>	'payroll_id',
				'default'	=>	'',
				'display'	=>	'ld_custom_reporting_column'
			);
		}
		
	} else if ( $data_slug == 'user-quizzes' ) {
		// Example on how to change the output column header label used. 
		//if ( isset( $data_headers['quiz_time_spent'] ) ) {
		//	$data_headers['quiz_time_spent']['label'] = 'Test Time Used';
		//}
		//if ( isset( $data_headers['quiz_points_total'] ) ) {
		//	$data_headers['quiz_points_total']['label'] = 'Test Points Total';
		//}
	}

	// Always return $data_headers
	return $data_headers;
}, 50, 2 );


/** 
 * This is an exmaple of a custom reporting column. The function is setup via the 'learndash_data_reports_headers' filter to add the custom report header.
 *
 * @param $column_value The current value of the header. Default is empty ''.
 * @param $column_key stirng This is the unique column key your assigned when adding the report header via the 'learndash_data_reports_headers' hook. 
 * @param $report_item object This is the activity object. This contains some reference information like course_id, course title, lesson_id, lesson_title, etc. 
 * @param $report_user object This is the user object associated with the $report_item. For example the user id is $report_user->ID
 *
 * @return $column_value string
 */
function ld_custom_reporting_column( $column_value = '', $column_key, $report_item, $report_user ) {
	
	switch( $column_key ) {
		case 'customer_id':
			if ( $report_user instanceof WP_User ) {
				$column_value = get_user_meta( $report_user->ID, 'customer_id', true );
			}
			break;

		case 'payroll_id':
			if ( $report_user instanceof WP_User ) {
				$column_value = get_user_meta( $report_user->ID, 'payroll_id', true );
			}
			break;

	}
	
	// always return $column_value
	return $column_value;
}

Comments (0)