Filter
uncanny-automator
uap_option_all_ld_lessons
Filters the array of all LearnDash lessons before they are displayed to users, allowing customization.
add_filter( 'uap_option_all_ld_lessons', $callback, 10, 1 );
Description
Filters the options array for selecting all LearnDash lessons, used when building automations. Developers can modify the available lesson options, their input types, or relevant tokens. This hook fires during Uncanny Automator's LearnDash integration setup for lesson-related triggers and actions.
Usage
add_filter( 'uap_option_all_ld_lessons', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter likely holds the value of the option being filtered, which in this case, refers to all available LearnDash lessons.
Return Value
The filtered value.
Examples
<?php
/**
* Example of using the 'uap_option_all_ld_lessons' filter to modify lesson options.
* This example demonstrates how to add a custom field to the lesson options
* or potentially filter the available lessons if the $option variable represented
* a list of lessons. In this specific context, $option is an array representing
* a single lesson's configuration.
*
* For demonstration purposes, we'll assume we want to add an extra descriptive
* field to the lesson option configuration if a certain condition is met.
*/
add_filter( 'uap_option_all_ld_lessons', 'my_custom_ld_lesson_options_filter', 10, 1 );
function my_custom_ld_lesson_options_filter( $option ) {
// Check if the lesson option is for a specific course or lesson ID
// In a real scenario, you might have logic here to determine if you need to modify this option.
// For this example, let's say we want to add a custom description if the lesson ID is '123'.
// Note: The original source code passes $option_code to get_lesson_relevant_tokens,
// but the filter hook itself only receives $option. We'll assume $option contains
// enough information or that we can infer the context.
// A more realistic scenario might involve checking a specific key within $option.
// Let's assume for the sake of demonstration that $option is an array and might contain 'value' or 'label'.
// If the $option array represents the configuration for a specific lesson,
// and we want to add a custom field to it.
if ( isset( $option['value'] ) && $option['value'] == '123' ) {
// Add a custom field to the lesson option configuration.
// This field might be used by Uncanny Automator for further customization or display.
$option['custom_extra_data'] = array(
'info' => esc_html__( 'This is a specially highlighted lesson.', 'my-text-domain' ),
'highlight' => true,
);
}
// It's also possible that the intention of the hook is to filter the *list* of lessons
// if $option was previously a multi-dimensional array of lesson options.
// However, based on the provided source context, $option appears to be a single
// lesson's configuration array. If it *were* a list, the logic would look different:
/*
if ( is_array( $option ) && ! empty( $option ) && is_array( reset( $option ) ) ) {
// Assuming $option is an array of lesson option arrays
$filtered_lessons = array_filter( $option, function( $lesson_config ) {
// Example: Filter out lessons with a specific status or ID
return isset( $lesson_config['status'] ) && $lesson_config['status'] !== 'draft';
} );
return $filtered_lessons;
}
*/
// Always return the modified (or unmodified) 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:282
public function all_ld_lessons( $label = null, $any_lesson = true, $option_code = 'LDLESSON' ) {
if ( ! $label ) {
$label = esc_attr_x( 'Lesson', 'Learndash', 'uncanny-automator' );
}
$args = array(
'post_type' => 'sfwd-lessons',
'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_lesson, esc_attr_x( 'Any lesson', 'Learndash', 'uncanny-automator' ) );
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'options' => $options,
'relevant_tokens' => wp_list_pluck( $this->get_lesson_relevant_tokens( 'trigger', $option_code ), 'name' ),
'custom_value_description' => esc_html_x( 'Lesson ID', 'LearnDash', 'uncanny-automator' ),
);
return apply_filters( 'uap_option_all_ld_lessons', $option );
}