Filter uncanny-automator

automator_set_learndash_course_{$type}_tokens

Filters LearnDash tokens for automator actions, allowing modification before they are used.

add_filter( 'automator_set_learndash_course_{$type}_tokens', $callback, 10, 1 );

Description

Fires to modify the available tokens for LearnDash course actions, specifically for `$type` (e.g., 'completed', 'enrolled'). Developers can add custom tokens or alter existing ones, providing dynamic course data for automations. Use this to enrich trigger or action data with specific course-related information.


Usage

add_filter( 'automator_set_learndash_course_{$type}_tokens', 'your_function_name', 10, 1 );

Parameters

$tokens (mixed)
This parameter contains an array of available tokens for the LearnDash course integration.

Return Value

The filtered value.


Examples

/**
 * Add a custom token for the Learndash course title when a new course is published.
 *
 * This example adds a new token named 'COURSE_PUBLISHED_TITLE' which will contain
 * the title of the course that was just published. This can be used in automations
 * to send notifications or perform other actions based on the published course title.
 *
 * @param array $tokens The existing array of tokens for Learndash courses.
 * @param string $type The type of token (e.g., 'trigger', 'action').
 * @return array The modified array of tokens.
 */
add_filter( 'automator_set_learndash_course_trigger_tokens', function( $tokens, $type ) {

    // Assuming $tokens is an array of arrays where each inner array represents a token.
    // We're adding a new token keyed by a unique identifier.
    // The actual value of $tokens might be dynamic and depend on the context from
    // where apply_filters is called. We'll add a placeholder if the structure allows.

    // In a real scenario, you'd likely need to access some global or passed variable
    // to get the actual title of the *newly published* course. Since this hook is
    // within a function that sets up tokens, we might not have the exact course ID
    // here directly unless it's passed as an additional argument (which it isn't in the hook signature).
    // For demonstration, let's assume we are within a context where we know the current course ID.
    // In a real LearnDash integration, this might come from a global or a passed parameter in the calling function.

    // For this example, let's pretend we're in a context where the currently processed course ID is known.
    // This is a simplification for demonstration.
    // A more robust solution would likely involve getting the course ID from the `$type` or a related context
    // if the filter was designed to pass more arguments.

    // Let's assume for this *specific* example, that the `$tokens` array structure allows adding new keys.
    // And we are adding a token for "Course Published Title" when the trigger is course published.

    // If the $type is 'trigger' and we are adding a token that relates to a published course:
    if ( 'trigger' === $type ) {
        // In a real scenario, you'd fetch the actual course title based on context.
        // For example, if the function calling this filter had access to the course ID.
        // Since we don't have the ID directly here in the filter callback without additional arguments,
        // this is a simulated addition.
        // A practical implementation might involve checking the `$tokens` array for existing keys
        // that are related to the course being processed and then adding a new one.

        // Example: Adding a hypothetical 'COURSE_PUBLISHED_TITLE' token.
        // The structure of $tokens can vary, but typically it's an associative array.
        // The key is the token identifier, and the value is an array with 'name' and 'type'.
        $tokens['COURSE_PUBLISHED_TITLE'] = array(
            'name' => esc_attr_x( 'Published Course Title', 'LearnDash Token', 'uncanny-automator' ),
            'type' => 'text',
            // In a real scenario, you might also add a 'callback' or 'value_callback'
            // to dynamically fetch the actual title when the token is used in an automation.
            // For this example, we're just defining the token structure.
        );
    }

    return $tokens;

}, 10, 2 ); // 10 is the priority, 2 is the number of accepted arguments ($tokens, $type).

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:203

public function get_course_relevant_tokens( $type = 'trigger', $option_code = 'LDCOURSE' ) {

		$tokens = array(
			$option_code                    => array(
				'name' => esc_attr_x( 'Course title', 'LearnDash Token', 'uncanny-automator' ),
				'type' => 'text',
			),
			$option_code . '_ID'            => array(
				'name' => esc_attr_x( 'Course ID', 'LearnDash Token', 'uncanny-automator' ),
				'type' => 'int',
			),
			$option_code . '_STATUS'        => array(
				'name' => esc_attr_x( 'Course status', 'LearnDash Token', 'uncanny-automator' ),
				'type' => 'text',
			),
			$option_code . '_ACCESS_EXPIRY' => array(
				'name' => esc_attr_x( 'Course access expiry date', 'LearnDash Token', 'uncanny-automator' ),
				'type' => 'text',
			),
			$option_code . '_URL'           => array(
				'name' => esc_attr_x( 'Course URL', 'LearnDash Token', 'uncanny-automator' ),
				'type' => 'text',
			),
			$option_code . '_THUMB_ID'      => array(
				'name' => esc_attr_x( 'Course featured image ID', 'LearnDash Token', 'uncanny-automator' ),
				'type' => 'int',
			),
			$option_code . '_THUMB_URL'     => array(
				'name' => esc_attr_x( 'Course featured image URL', 'LearnDash Token', 'uncanny-automator' ),
				'type' => 'text',
			),
		);

		return apply_filters( "automator_set_learndash_course_{$type}_tokens", $tokens );
	}

Scroll to Top