Filter
uncanny-automator
automator_set_learndash_topic_{$type}_tokens
Filters the available tokens for Learndash topics when creating or editing an automation.
add_filter( 'automator_set_learndash_topic_{$type}_tokens', $callback, 10, 1 );
Description
Fires when LearnDash topic tokens are being prepared for an automator action, allowing developers to add, remove, or modify tokens based on topic type. This filter provides fine-grained control over available LearnDash topic data for integration.
Usage
add_filter( 'automator_set_learndash_topic_{$type}_tokens', 'your_function_name', 10, 1 );
Parameters
-
$tokens(mixed) - This parameter contains a mixed array of tokens that can be used with the LearnDash topic.
Return Value
The filtered value.
Examples
add_filter( 'automator_set_learndash_topic_completion_tokens', 'my_custom_learndash_topic_completion_tokens', 10, 1 );
/**
* Adds a custom token to the LearnDash topic completion tokens.
*
* This function demonstrates how to add a new token that represents the
* completion date of a LearnDash topic for a specific user.
*
* @param array $tokens The array of existing tokens.
* @return array The modified array of tokens including the new custom token.
*/
function my_custom_learndash_topic_completion_tokens( $tokens ) {
// Assuming $tokens array structure is similar to the example provided.
// We'll add a new token key, for example, 'TOPIC_COMPLETION_DATE'.
// First, let's try to get the actual completion date if possible.
// This is a placeholder. In a real scenario, you'd likely need to
// fetch this data from LearnDash's database or use its API if available.
// For this example, we'll simulate it.
// Let's imagine we have a way to get the completion date.
// For this example, we'll assume we have a function `get_learndash_topic_completion_date_for_user( $topic_id, $user_id )`
// that returns the date or false. However, this filter doesn't provide
// $topic_id or $user_id directly. The $tokens array itself might contain them,
// or we'd need to hook into another filter that provides them.
// For the sake of a realistic example within the constraint of this hook's signature,
// we'll add a static placeholder token. If the context within Uncanny Automator
// provides the $topic_id and $user_id to the function that eventually calls this filter,
// then you could dynamically generate the token value.
// Let's add a token for the completion date, assuming the context provides a way
// to access the current topic ID and user ID, perhaps via a global or another parameter
// that isn't directly exposed by this filter's signature.
// A more practical scenario might be if the $tokens array itself already
// contains context like topic ID and user ID.
// Example: If $tokens already contains 'LEARNDASH_TOPIC_ID' and 'USER_ID'
$topic_id = isset( $tokens['LEARNDASH_TOPIC_ID'] ) ? $tokens['LEARNDASH_TOPIC_ID'] : 0;
$user_id = isset( $tokens['USER_ID'] ) ? $tokens['USER_ID'] : 0;
$completion_date_value = '';
if ( $topic_id && $user_id ) {
// In a real implementation, you would fetch this:
// $completion_date = get_learndash_topic_completion_date_for_user( $topic_id, $user_id );
// For this example, we'll use a dummy value.
$completion_date_value = date( 'Y-m-d H:i:s' ); // Placeholder completion date
}
// Add the new token to the $tokens array.
// The key should be unique and descriptive.
$tokens['MY_CUSTOM_TOPIC_COMPLETION_DATE'] = array(
'name' => esc_attr_x( 'Topic Completion Date (Custom)', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'text',
// If you had dynamic data, you could pass it here,
// but typically tokens are defined by their *availability* and
// the actual value is retrieved when the token is used in an automation.
// For this definition, we'll just define its presence.
// The actual value hydration happens elsewhere.
'value' => '', // Value is usually determined at runtime by Uncanny Automator
);
return $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:426
public function get_topic_relevant_tokens( $type = 'trigger', $option_code = 'LDTOPIC' ) {
$tokens = array(
$option_code => array(
'name' => esc_attr_x( 'Topic title', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'text',
),
$option_code . '_ID' => array(
'name' => esc_attr_x( 'Topic ID', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'int',
),
$option_code . '_URL' => array(
'name' => esc_attr_x( 'Topic URL', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'text',
),
$option_code . '_THUMB_ID' => array(
'name' => esc_attr_x( 'Topic featured image ID', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'int',
),
$option_code . '_THUMB_URL' => array(
'name' => esc_attr_x( 'Topic featured image URL', 'LearnDash Token', 'uncanny-automator' ),
'type' => 'text',
),
);
return apply_filters( "automator_set_learndash_topic_{$type}_tokens", $tokens );
}