Filter
uncanny-automator
uap_option_list_mycred_badges
Filters the myCred badges displayed within the user's badge list, allowing for customization and control.
add_filter( 'uap_option_list_mycred_badges', $callback, 10, 1 );
Description
Fires when generating the list of myCred badges for automator actions. Developers can filter the `$option` array to modify the badge selection parameters, such as adding custom badges or altering the display text. This hook is useful for customizing how myCred badges are presented in Uncanny Automator.
Usage
add_filter( 'uap_option_list_mycred_badges', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter contains the badge type identifier, which is typically 'MYCREDBADGETYPES'.
Return Value
The filtered value.
Examples
/**
* Example: Filter the list of myCred badges to exclude badges with a specific ID.
*
* This function demonstrates how to hook into 'uap_option_list_mycred_badges'
* to modify the array of myCred badges before they are displayed in a Uncanny Automator
* settings field. In this example, we're removing any badge whose ID is 'admin-special-badge'.
*
* @param array $option The original array of badge options.
* @return array The modified array of badge options.
*/
add_filter(
'uap_option_list_mycred_badges',
function ( $option ) {
// Ensure $option is an array and has an 'options' key before proceeding.
if ( ! is_array( $option ) || ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
return $option; // Return original if structure is unexpected.
}
// Define the badge ID to exclude.
$badge_id_to_exclude = 'admin-special-badge';
// Filter out the badge with the specific ID.
$option['options'] = array_filter(
$option['options'],
function ( $badge_data ) use ( $badge_id_to_exclude ) {
// Assuming badge data is an array with an 'id' key.
// Adjust the key access if your badge data structure is different.
return isset( $badge_data['id'] ) && $badge_data['id'] !== $badge_id_to_exclude;
}
);
// Re-index the array after filtering to ensure clean sequential keys.
$option['options'] = array_values( $option['options'] );
// Return the modified options array.
return $option;
},
10, // Priority: 10 is the default, adjust as needed.
1 // Accepted arguments: This filter only passes one 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/mycred/helpers/mycred-helpers.php:216
public function list_mycred_badges( $label = null, $option_code = 'MYCREDBADGETYPES', $args = array() ) {
if ( ! $label ) {
$label = esc_attr__( 'Badges', 'uncanny-automator' );
}
$token = key_exists( 'token', $args ) ? $args['token'] : false;
$is_ajax = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
$end_point = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
$include_all = key_exists( 'include_all', $args ) ? $args['include_all'] : false;
$options = array();
if ( $include_all ) {
$options['ua-all-mycred-badges'] = esc_attr__( 'All badges', 'uncanny-automator' );
}
/*if ( Automator()->helpers->recipe->load_helpers ) {
$posts = get_posts( [
'post_type' => 'mycred_badge',
'posts_per_page' => 9999,
'post_status' => 'publish'
] );
if ( ! empty( $posts ) ) {
foreach ( $posts as $post ) {
if ( $post->post_type === 'mycred_badge' ) {
$options[ $post->ID ] = $post->post_title;
}
}
}
}*/
$query_args = array(
'post_type' => 'mycred_badge',
'posts_per_page' => 9999,
'post_status' => 'publish',
);
$options = Automator()->helpers->recipe->wp_query( $query_args );
$type = 'select';
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => $type,
'required' => true,
'supports_tokens' => $token,
'is_ajax' => $is_ajax,
'fill_values_in' => $target_field,
'endpoint' => $end_point,
'options' => $options,
'custom_value_description' => esc_attr__( 'Badge ID', 'uncanny-automator' ),
);
return apply_filters( 'uap_option_list_mycred_badges', $option );
}