Filter
uncanny-automator
uap_option_all_ld_quiz
Filters the options array for all LearnDash quiz settings, allowing modification before saving or display.
add_filter( 'uap_option_all_ld_quiz', $callback, 10, 1 );
Description
Filters the array of options for the LearnDash quiz selection in Uncanny Automator. Developers can modify the available quiz options, their labels, or add custom logic before they are displayed. This hook fires when Uncanny Automator is preparing the quiz selection field for use in a recipe.
Usage
add_filter( 'uap_option_all_ld_quiz', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter represents the option that will be filtered, typically a value or label related to a LearnDash quiz.
Return Value
The filtered value.
Examples
<?php
/**
* Example: Modify the options for the 'uap_option_all_ld_quiz' filter.
* This example adds a custom option to the LearnDash quiz selection if a specific condition is met.
*/
add_filter( 'uap_option_all_ld_quiz', 'my_custom_ld_quiz_options', 10, 1 );
function my_custom_ld_quiz_options( $option ) {
// Check if the current user has a specific capability.
// Replace 'manage_options' with the actual capability you want to check.
if ( current_user_can( 'manage_options' ) ) {
// Add a new custom option to the existing options array.
// 'custom_quiz_id' is the value that will be returned.
// 'Custom Quiz Option' is the human-readable label displayed in the select field.
$option['options']['custom_quiz_id'] = esc_html__( 'Custom Quiz Option', 'my-text-domain' );
// Optionally, you could also modify other parts of the $option array,
// for example, change the 'custom_value_description' if needed.
// $option['custom_value_description'] = esc_html_x( 'Custom Quiz Value', 'LearnDash', 'uncanny-automator' );
}
// Always return the modified (or unmodified) $option array.
return $option;
}
/**
* Example: Filter LearnDash quiz data to exclude quizzes with a specific tag.
* This assumes that the $option['options'] array contains arrays where
* each array represents a quiz and has a 'tag' key.
*/
add_filter( 'uap_option_all_ld_quiz', 'exclude_quizzes_by_tag_from_ld_quiz_options', 10, 1 );
function exclude_quizzes_by_tag_from_ld_quiz_options( $option ) {
// Define the tag to exclude.
$tag_to_exclude = 'hidden_from_automator';
// Filter the options array to remove quizzes with the specified tag.
if ( isset( $option['options'] ) && is_array( $option['options'] ) ) {
$filtered_options = array_filter( $option['options'], function( $quiz_data ) use ( $tag_to_exclude ) {
// Ensure $quiz_data is an array and has a 'tag' key.
if ( is_array( $quiz_data ) && isset( $quiz_data['tag'] ) ) {
// Return true if the quiz tag is NOT the one to exclude.
return $quiz_data['tag'] !== $tag_to_exclude;
}
// If $quiz_data is not in the expected format, keep it by default.
return true;
} );
// Update the options in the $option array with the filtered results.
$option['options'] = $filtered_options;
}
// Return the modified $option array.
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/learndash/helpers/learndash-helpers.php:626
public function all_ld_quiz( $label = null, $option_code = 'LDQUIZ', $any_option = true ) {
if ( ! $label ) {
$label = esc_attr_x( 'Quiz', 'Learndash', 'uncanny-automator' );
}
$args = array(
'post_type' => 'sfwd-quiz',
'posts_per_page' => 9999, // 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_attr_x( 'Any quiz', 'Learndash', 'uncanny-automator' ) );
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'options' => $options,
'relevant_tokens' => wp_list_pluck( $this->get_quiz_relevant_tokens( 'trigger', $option_code ), 'name' ),
'custom_value_description' => esc_html_x( 'Quiz ID', 'LearnDash', 'uncanny-automator' ),
);
return apply_filters( 'uap_option_all_ld_quiz', $option );
}