Filter
uncanny-automator
uap_option_all_wpcw_courses
Filters all WP Courseware options before they are retrieved for display, allowing modification.
add_filter( 'uap_option_all_wpcw_courses', $callback, 10, 1 );
Description
Filters the array of available options for selecting WP Courseware courses in Uncanny Automator, including course title, ID, and URL. Developers can use this hook to add, remove, or modify the available course tokens for triggers and actions. This filter fires when Uncanny Automator is preparing the list of available WP Courseware course options.
Usage
add_filter( 'uap_option_all_wpcw_courses', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the 'uap_option_all_wpcw_courses' filter to modify
* the available options for selecting a WP Courseware course in Uncanny Automator.
*
* This example adds a new token representing the course's category.
*/
add_filter( 'uap_option_all_wpcw_courses', function( $options ) {
// Check if the provided $options is an array and has the expected structure.
if ( ! is_array( $options ) || ! isset( $options['relevant_tokens'] ) || ! is_array( $options['relevant_tokens'] ) ) {
return $options; // Return original if structure is unexpected.
}
// Assuming $option_code is available within the context where this filter is applied.
// In a real scenario, you might need to infer or pass this value.
// For this example, let's assume it's 'wpcw_course'.
$option_code = 'wpcw_course';
// Add a new token for the course category.
// We need to fetch the category for each course if it's not directly available.
// This is a simplified example. In a real-world scenario, you might need to iterate
// through the actual WP Courseware courses to get their categories.
// Let's imagine we have a way to get all WP Courseware courses and their categories.
// This is a placeholder for actual WP Courseware API calls.
$wp_courseware_courses = array(
1 => array( 'title' => 'Introduction to PHP', 'category' => 'Programming' ),
2 => array( 'title' => 'Advanced WordPress Development', 'category' => 'WordPress' ),
3 => array( 'title' => 'Beginner's Guide to Design', 'category' => 'Design' ),
);
$course_category_tokens = array();
foreach ( $wp_courseware_courses as $course_id => $course_data ) {
// Create a unique token for each course's category.
// We'll use a naming convention that implies it's specific to a course.
// For example: wpcw_course_CATEGORY_1 (assuming course ID is 1)
$category_token_key = $option_code . '_category_' . $course_id;
$course_category_tokens[ $category_token_key ] = sprintf(
esc_attr__( 'Category of %s', 'uncanny-automator' ),
esc_html( $course_data['title'] )
);
}
// Merge the new category tokens with the existing relevant tokens.
$options['relevant_tokens'] = array_merge( $options['relevant_tokens'], $course_category_tokens );
// You could also modify or add other properties to the $options array if needed.
// For example, if there was a 'description' key, you could add to it.
return $options;
}, 10, 1 ); // Priority 10, accepts 1 argument.
?>
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/wp-courseware/helpers/wp-courseware-helpers.php:88
public function all_wpcw_courses( $label = null, $option_code = 'WPCW_COURSE', $any_option = true ) {
if ( ! $label ) {
$label = esc_attr__( 'Course', 'uncanny-automator' );
}
$args = array(
'post_type' => 'wpcw_course',
'posts_per_page' => 9999,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish',
);
$options = Automator()->helpers->recipe->options->wp_query( $args, $any_option, esc_attr__( 'Any course', '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_attr__( 'Course title', 'uncanny-automator' ),
$option_code . '_ID' => esc_attr__( 'Course ID', 'uncanny-automator' ),
$option_code . '_URL' => esc_attr__( 'Course URL', 'uncanny-automator' ),
),
);
return apply_filters( 'uap_option_all_wpcw_courses', $option );
}