Filter Uncanny Redemption Codes

ulc_view_codes_columns

Filters the columns displayed in the Uncanny Codes view, allowing customization of the table headers.

add_filter( 'ulc_view_codes_columns', $callback, 10, 1 );

Description

Modify the columns displayed in the "View Codes" admin table. This filter allows developers to add, remove, or reorder columns by returning a modified array of column slugs and their translated titles.


Usage

add_filter( 'ulc_view_codes_columns', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Example of adding a custom column to the Uncanny LearnDash Codes view.
 *
 * This function demonstrates how to add a new column to the table displaying
 * coupon codes in the Uncanny LearnDash Codes plugin. In this example, we're
 * adding a column to display the course the coupon is associated with.
 *
 * @param array $columns The existing array of columns.
 * @return array The modified array of columns.
 */
add_filter(
	'ulc_view_codes_columns',
	function ( $columns ) {
		// Add a new column for 'course_title' after the 'coupon' column.
		// We need to reconstruct the array to insert the new column at the desired position.
		$new_columns = array();

		foreach ( $columns as $key => $value ) {
			$new_columns[ $key ] = $value;
			if ( 'coupon' === $key ) {
				$new_columns['course_title'] = esc_html__( 'Course', 'uncanny-learndash-codes' );
			}
		}

		return $new_columns;
	},
	10, // Priority
	1  // Accepted arguments
);

/**
 * Example of modifying the data displayed in a custom column.
 *
 * This function demonstrates how to add data for the newly added 'course_title' column.
 * It hooks into 'ulc_view_codes_custom_columns'.
 *
 * @param string $column_name The name of the current column.
 * @param int    $code_id     The ID of the coupon code row.
 */
add_action(
	'ulc_view_codes_custom_columns',
	function ( $column_name, $code_id ) {
		if ( 'course_title' === $column_name ) {
			// Retrieve coupon data.
			$coupon_data = uncanny_learndash_codes_get_coupon_data( $code_id );

			// If coupon data exists and has a course ID, try to get the course title.
			if ( $coupon_data && isset( $coupon_data['course_id'] ) && absint( $coupon_data['course_id'] ) > 0 ) {
				$course_id = absint( $coupon_data['course_id'] );
				$course_title = get_the_title( $course_id );
				echo esc_html( $course_title );
			} else {
				echo esc_html__( 'N/A', 'uncanny-learndash-codes' );
			}
		}
	},
	10, // Priority
	2   // Accepted arguments
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/admin/class-view-codes.php:63

public function get_columns() {
		return apply_filters(
			'ulc_view_codes_columns',
			array(
				'cb'            => '<input type="checkbox" />',
				'coupon'        => esc_html__( 'Code', 'uncanny-learndash-codes' ),
				'user_nicename' => esc_html__( 'Redeemed user', 'uncanny-learndash-codes' ),
				'used_date'     => esc_html__( 'Redeemed date', 'uncanny-learndash-codes' ),
				'expire_date'   => esc_html__( 'Expiry date', 'uncanny-learndash-codes' ),
				'actions'       => esc_html__( 'Action', 'uncanny-learndash-codes' ),
			)
		);
	}

Scroll to Top