Filter uncanny-automator

automator_hydrate_learndash_action_token_{$token}

Filters LearnDash action token data when a token is being hydrated for automation.

add_filter( 'automator_hydrate_learndash_action_token_{$token}', $callback, 10, 2 );

Description

Fires when hydrating LearnDash action tokens, allowing customization of the token's resolved value. Developers can filter the returned value for specific tokens like `_THUMB_ID` or `_THUMB_URL` based on course and user IDs. This hook is crucial for dynamic token resolution within LearnDash automations.


Usage

add_filter( 'automator_hydrate_learndash_action_token_{$token}', 'your_function_name', 10, 2 );

Parameters

$course_id (mixed)
This parameter contains the value of the LearnDash token being hydrated.
$user_id (mixed)
This parameter represents the unique identifier of the LearnDash course.

Return Value

The filtered value.


Examples

<?php
/**
 * Example: Modify the LearnDash course title token for automator.
 *
 * This filter hook allows modification of the hydrated token value before it's used in an automation.
 * Here, we're specifically targeting a hypothetical token that represents the LearnDash course title.
 * We'll append a prefix to the course title to demonstrate how to alter the token's value.
 *
 * @param mixed  $value      The current value of the token (passed as '' by default).
 * @param int    $course_id  The ID of the LearnDash course.
 * @param int    $user_id    The ID of the user.
 *
 * @return string The modified course title.
 */
add_filter( 'automator_hydrate_learndash_action_token__course_title', function ( $value, $course_id, $user_id ) {
	// Ensure we have a valid course ID before proceeding.
	if ( ! $course_id || ! is_numeric( $course_id ) ) {
		return $value; // Return original value if course ID is invalid.
	}

	// Retrieve the course title.
	$course_title = get_the_title( $course_id );

	// If we successfully retrieved a title, prepend a prefix.
	if ( ! empty( $course_title ) ) {
		return 'My Automator Course: ' . $course_title;
	}

	// Return the original value if no course title was found.
	return $value;
}, 10, 3 ); // Priority 10, accepts 3 arguments.
?>

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:243
src/integrations/learndash/helpers/learndash-helpers.php:352
src/integrations/learndash/helpers/learndash-helpers.php:460
src/integrations/learndash/helpers/learndash-helpers.php:586
src/integrations/learndash/helpers/learndash-helpers.php:737

public function hydrate_ld_course_action_tokens( $course_id, $user_id, $action_meta ) {

		$relevant_tokens = $this->get_course_relevant_tokens( 'action', $action_meta );
		$tokens          = array();
		foreach ( $relevant_tokens as $token => $config ) {
			switch ( $token ) {
				case $action_meta:
					$tokens[ $token ] = get_the_title( $course_id );
					break;
				case $action_meta . '_ID':
					$tokens[ $token ] = $course_id;
					break;
				case $action_meta . '_STATUS':
					$tokens[ $token ] = learndash_course_status( $course_id, $user_id );
					break;
				case $action_meta . '_ACCESS_EXPIRY':
					$tokens[ $token ] = learndash_adjust_date_time_display( ld_course_access_expires_on( $course_id, $user_id ) );
					break;
				case $action_meta . '_URL':
					$tokens[ $token ] = get_permalink( $course_id );
					break;
				case $action_meta . '_THUMB_ID':
					$tokens[ $token ] = get_post_thumbnail_id( $course_id );
					break;
				case $action_meta . '_THUMB_URL':
					$tokens[ $token ] = get_the_post_thumbnail_url( $course_id );
					break;
				default:
					$tokens[ $token ] = apply_filters( "automator_hydrate_learndash_action_token_{$token}", '', $course_id, $user_id );
					break;
			}
		}

		return $tokens;
	}

Scroll to Top