Filter
uncanny-automator
automator_set_learndash_lesson_{$type}_tokens
Filters Learndash lesson tokens when a lesson is processed for automator actions.
add_filter( 'automator_set_learndash_lesson_{$type}_tokens', $callback, 10, 1 );
Description
Fires after LearnDash lesson tokens are prepared, allowing developers to add, modify, or remove tokens for specific lesson types. This filter is ideal for custom LearnDash integrations to provide unique dynamic content options within Uncanny Automator recipes, particularly for lesson-related actions.
Usage
add_filter( 'automator_set_learndash_lesson_{$type}_tokens', 'your_function_name', 10, 1 );
Parameters
-
$tokens(mixed) - This parameter contains an array of tokens relevant to LearnDash lessons, including their titles and IDs.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to filter the available tokens for LearnDash lessons.
*
* This example adds a custom token for the lesson's associated course title
* to the list of available tokens when a user completes a lesson.
*
* @param array $tokens An array of existing tokens for LearnDash lessons.
* @param string $type The type of token set being filtered (e.g., 'complete', 'start').
* @return array The modified array of tokens.
*/
function my_automator_add_learndash_lesson_tokens( $tokens, $type ) {
// Only add the custom token if the token type is 'complete'.
// This is just an example; you might want to apply it to other types.
if ( 'complete' === $type ) {
// Add a new token for the course title.
// The key '_COURSE_TITLE' is a custom identifier.
// The 'name' is what appears in the Uncanny Automator token picker.
// The 'type' indicates the data type of the token.
$tokens['YOUR_PREFIX_LESSON_COURSE_TITLE'] = array(
'name' => esc_attr_x( 'Lesson's Course Title', 'LearnDash Token', 'my-text-domain' ),
'type' => 'text',
);
}
// Always return the $tokens array, whether modified or not.
return $tokens;
}
add_filter( 'automator_set_learndash_lesson_tokens', 'my_automator_add_learndash_lesson_tokens', 10, 2 );
/**
* Example of how to retrieve and use a custom token.
*
* This function would be part of your Uncanny Automator action or trigger
* where you need to access the custom lesson course title token.
*
* @param int $lesson_id The ID of the LearnDash lesson.
* @param int $user_id The ID of the user.
* @return string The course title for the given lesson.
*/
function my_automator_get_lesson_course_title( $lesson_id, $user_id ) {
// Ensure LearnDash is active.
if ( ! class_exists( 'LearnDash_Data_Factory' ) ) {
return '';
}
// Get the course ID associated with the lesson.
$course_id = learndash_get_course_id_by_lesson_id( $lesson_id );
// If no course is found, return an empty string.
if ( ! $course_id ) {
return '';
}
// Get the course title.
$course_title = get_the_title( $course_id );
return $course_title;
}
// Note: This function 'my_automator_get_lesson_course_title' would be called
// internally by Uncanny Automator when a recipe uses the 'YOUR_PREFIX_LESSON_COURSE_TITLE' token.
// You don't typically call add_filter on a function that *retrieves* data.
// The add_filter is for *defining* the available tokens.
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:318
public function get_lesson_relevant_tokens( $type = 'trigger', $option_code = 'LDLESSON' ) {
$tokens = array(
$option_code => array(
'name' => esc_attr_x( 'Lesson title', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'text',
),
$option_code . '_ID' => array(
'name' => esc_attr_x( 'Lesson ID', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'int',
),
$option_code . '_URL' => array(
'name' => esc_attr_x( 'Lesson URL', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'text',
),
$option_code . '_THUMB_ID' => array(
'name' => esc_attr_x( 'Lesson featured image ID', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'int',
),
$option_code . '_THUMB_URL' => array(
'name' => esc_attr_x( 'Lesson featured image URL', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'text',
),
);
return apply_filters( "automator_set_learndash_lesson_{$type}_tokens", $tokens );
}