Filter
uncanny-automator
uap_option_all_lp_courses
Filters the array of all LearnPress courses before they are displayed or processed.
add_filter( 'uap_option_all_lp_courses', $callback, 10, 1 );
Description
Fires after all LearnPress courses are retrieved and formatted for use in Uncanny Automator tokens. Developers can use this filter to add custom course data or modify existing course details before they are displayed as token options. This hook is crucial for extending LearnPress course integration within Uncanny Automator.
Usage
add_filter( 'uap_option_all_lp_courses', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter is the option label that will be displayed in the dropdown.
Return Value
The filtered value.
Examples
add_filter( 'uap_option_all_lp_courses', 'my_custom_learnpress_courses_options', 10, 1 );
/**
* Adds a custom option to the LearnPress courses selection in Uncanny Automator.
* This example shows how to add a new relevant token for a specific course.
*
* @param array $option The current options array for LearnPress courses.
* @return array The modified options array with the new custom token.
*/
function my_custom_learnpress_courses_options( $option ) {
// Assuming $option_code is available in the scope where this filter is applied.
// In a real scenario, you might need to find or define $option_code.
// For this example, let's imagine $option_code is 'lp_course_details'.
$option_code = 'lp_course_details'; // Example $option_code
// Check if the 'relevant_tokens' key exists and is an array
if ( isset( $option['relevant_tokens'] ) && is_array( $option['relevant_tokens'] ) ) {
// Add a new custom token, for example, to display the instructor's name.
// This assumes you have a way to retrieve the instructor's name for a course.
// We'll use a placeholder for the actual retrieval logic.
$option['relevant_tokens'][ $option_code . '_INSTRUCTOR' ] = esc_html_x( 'Course Instructor Name', 'Learnpress', 'uncanny-automator' );
// You could also potentially add custom fields or modify existing ones.
// For instance, let's add a placeholder for a specific course completion status.
// This is purely illustrative as the original code doesn't suggest this.
// $option['relevant_tokens'][ $option_code . '_COMPLETION_STATUS' ] = esc_html_x( 'Specific Course Completion Status', 'Learnpress', 'uncanny-automator' );
}
// You could also add entirely new top-level keys if the hook allowed for it,
// but based on the source context, we're likely modifying the existing structure.
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/learnpress/helpers/learnpress-helpers.php:112
public function all_lp_courses( $label = null, $option_code = 'LPCOURSE', $any_option = true ) {
if ( ! $label ) {
$label = esc_html_x( 'Course', 'Learnpress', 'uncanny-automator' );
}
$args = array(
'post_type' => 'lp_course',
'posts_per_page' => 999, // 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_option, esc_html_x( 'Any course', 'Learnpress', 'uncanny-automator' ) );
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
// to setup example, lets define the value the child will be based on
'current_value' => false,
'validation_type' => 'text',
'options' => $options,
'relevant_tokens' => array(
$option_code => esc_html_x( 'Course title', 'Learnpress', 'uncanny-automator' ),
$option_code . '_ID' => esc_html_x( 'Course ID', 'Learnpress', 'uncanny-automator' ),
$option_code . '_URL' => esc_html_x( 'Course URL', 'Learnpress', 'uncanny-automator' ),
),
);
return apply_filters( 'uap_option_all_lp_courses', $option );
}