Filter uncanny-automator

uap_option_all_ld_courses

Filters the options array for all LearnDash courses, allowing modification before it's saved or displayed.

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

Description

Fires when retrieving the options for the LearnDash course selection in Uncanny Automator. Developers can filter the `$option` array to modify the input type, required status, options, relevant tokens, or custom value description for the course selection field, allowing for custom behavior or data manipulation.


Usage

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

Parameters

$option (mixed)
This parameter is the label used for the dropdown menu displaying LearnDash courses.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the list of LearnDash courses provided by Uncanny Automator.
 * This function will add a custom option to the beginning of the course list,
 * allowing users to select "All Courses" as a special option.
 *
 * @param array $option The original array of LearnDash course options.
 * @return array The modified array of LearnDash course options, including "All Courses".
 */
add_filter( 'uap_option_all_ld_courses', 'my_custom_all_ld_courses_filter', 10, 1 );

function my_custom_all_ld_courses_filter( $option ) {
	// Check if the $option is in the expected format (an array with 'options' key)
	if ( ! is_array( $option ) || ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
		return $option; // Return original if format is unexpected
	}

	// Create a new option for "All Courses"
	$all_courses_option = array(
		'value' => 'all_ld_courses', // A unique identifier for this option
		'label' => esc_html__( 'All LearnDash Courses', 'your-text-domain' ),
	);

	// Prepend the "All Courses" option to the beginning of the existing options array
	array_unshift( $option['options'], $all_courses_option );

	return $option;
}

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/integrations/learndash/helpers/learndash-helpers.php:147

public function all_ld_courses( $label = null, $option_code = 'LDCOURSE', $any_option = true, $include_relevant_tokens = true, $relevant_tokens = array() ) {

		if ( ! $label ) {
			$label = esc_attr_x( 'Course', 'Learndash', 'uncanny-automator' );
		}

		$args = array(
			'post_type'      => 'sfwd-courses',
			'posts_per_page' => 9999, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
			'orderby'        => 'title',
			'order'          => 'ASC',
			'post_status'    => 'publish',
		);

		$options = Automator()->helpers->recipe->options->wp_query( $args, $any_option, esc_attr_x( 'Any course', 'Learndash', 'uncanny-automator' ) );

		$courses_relevant_tokens = array();
		if ( $include_relevant_tokens ) {
			$courses_relevant_tokens = wp_list_pluck( $this->get_course_relevant_tokens( 'trigger', $option_code ), 'name' );

			if ( self::is_course_timer_activated() ) {
				$courses_relevant_tokens[ $option_code . '_COURSE_CUMULATIVE_TIME' ]    = esc_html_x( 'Course cumulative time', 'Learndash', 'uncanny-automator' );
				$courses_relevant_tokens[ $option_code . '_COURSE_TIME_AT_COMPLETION' ] = esc_html_x( 'Course time at completion', 'Learndash', 'uncanny-automator' );
			}

			if ( is_array( $relevant_tokens ) && ! empty( $relevant_tokens ) ) {
				$courses_relevant_tokens = array_merge( $courses_relevant_tokens, $relevant_tokens );
			}
		}

		$option = array(
			'option_code'              => $option_code,
			'label'                    => $label,
			'input_type'               => 'select',
			'required'                 => true,
			'options'                  => $options,
			'relevant_tokens'          => $courses_relevant_tokens,
			'custom_value_description' => esc_html_x( 'Course ID', 'LearnDash', 'uncanny-automator' ),
		);

		return apply_filters( 'uap_option_all_ld_courses', $option );
	}

Scroll to Top