0

There is this function that is part of the plugin EasyDigitalDownlaods' plugin:

public function category_dropdown( $name = 'edd_categories', $selected = 0 ) {
    $categories = get_terms( 'download_category', apply_filters( 'edd_category_dropdown', array() ) );
    $options    = array();

    foreach ( $categories as $category ) {
        $options[ absint( $category->term_id ) ] = esc_html( $category->name );
    }

    $category_labels = edd_get_taxonomy_labels( 'download_category' );
    $output          = $this->select( array(
        'name'             => $name,
        'selected'         => $selected,
        'options'          => $options,
        'show_option_all'  => sprintf( _x( 'All %s', 'plural: Example: "All Categories"', 'easy-digital-downloads' ), $category_labels['name'] ),
        'show_option_none' => false,
    ) );

    return $output;
}

I want to override it because it currently skip the empty categories due to the line 2:

$categories = get_terms( 'download_category', apply_filters( 'edd_category_dropdown', array() ) );

That needs to be changed to:

$categories = get_terms( 'download_category', apply_filters( 'edd_category_dropdown', array('hide_empty' => 0) ) );

I tried to add the following in my function.php but it seems that I don't understand how filtering works for this case:

function category_dropdown_unfiltered($array){
  $array = array('hide_empty' => 0);
  return $array;
}
add_filter( 'category_dropdown' , 'category_dropdown_unfiltered', 99, 1);
3
  • Oh I am filtering 'category_dropdown' instead of 'edd_category_dropdown'...
    – Niko
    Dec 19, 2023 at 12:57
  • you should write that as an answer rather than a comment, can't give you points for a comment!
    – Tom J Nowell
    Dec 19, 2023 at 14:25
  • I will give you these points, for taking the time to answer.
    – Niko
    Dec 19, 2023 at 16:33

1 Answer 1

0

This is the pattern for a filter.

Applying/using a filter on something:

$filtered_version = apply_filters( 'the name of the filter', $the_thing_being_filtered );

This is how you use a filter to modify something:

add_filter(
    'the name of the filter', 
    'name_of_a_function_that_will_modify_the_thing_being_filtered'
);

function name_of_a_function_that_will_modify_the_thing_being_filtered( $the_thing_being_filtered ) {
    // modify $the_thing_being_filtered here
    return $the_thing_being_filtered;
}

So in your examples it did not work because category_dropdown is not the name of the filter, edd_category_dropdown is the name and they do not match so they do not work.

Order also matters, you can't add a filter to something after it has happened without some form or time travel. WP won't wait until all filters and actions/hooks are added then evaluate it at the end, that's not how PHP works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.