Granted the following end result is a little convoluted, it really doesn’t need to be, but there was good reason for the complexity in my use case. I wanted to add the functionality of not displaying a specific default Category Widget line but I also had to keep in mind others may find this useful but may also want to choose a different category not to display.

Basically you need to do a little digging, but not too deep, into WordPress core to find what we need for this: the widget_categories_args filter hook. Once you have this hook sorted out you can easily create the callback function to exclude a specific category, or categories.

Let’s start with the basics:

function wpfa_hide_category_widget_list_items( $cat_args ) {

		/** @var array $cat_args - contains current list categories arguments */
		$cat_args = array_merge( $cat_args, array( 'exclude' => 1 ) ) );

		/** Send back the merged array excluding "Uncategorized" categories */

		return $cat_args;

}

We then simply use the callback as is:

add_filter( 'widget_categories_args', 'wpfa_hide_category_widget_list_items' );

OK, so that is all well and good but what if you want to make this a little more configurable and allow others to simply send their category choices programmatically?

We’ll need to refactor the array_merge statement so there is a way to modify the exclude parameter value. Why not just filter it like so?

$cat_args = array_merge( $cat_args, array( 'exclude' => apply_filters( 'wpfa_widget_exclude_list', '', 11 ) ) );

Although this is where the idea starts to get complex there is still an easy method to use it afterward. Let’s take a quick look at what we just did to the array_merge statement.

We change the absolute value from 1 to a filtered value that returns null, or in other words, the function will do nothing except take up space … unless you use wf_widget_exclude_list filter hook. We also are purposely setting the priority to run after wpfa_hide_category_widget_list_items has been called to ensure the filtered value is passed in the correct sequence.

Now, we can programmatically change the exclude parameter value with a single line of code (at least in cases where anonymous PHP functions are allowed), such as:

add_filter( 'wpfa_widget_exclude_list', function() { return 1; } );

For the entire code snippet you can use yourself see below:

To see the code click here.To hide the code click here.

	/**
	 * Hide Category Widget List Items
	 *
	 * Takes the current `wp_list_categories` arguments and adds (overwrites?)
	 * the exclude parameter value(s) stopping the filtered list of excluded
	 * categories from being displayed. This requires the the filter hook
	 * `opus_primus_category_widget_exclude_list` be used to hide the specific
	 * categories by their ID.
	 *
	 * @package WP_First_Aid
	 * @subpackage How_To
	 * @since   1.0
	 *
	 * @uses    apply_filters
	 *
	 * @param   $cat_args
	 *
	 * @example add_filter( 'wpfa_widget_exclude_list', function() { return 1; } );
	 *
	 * @return  array
	 */
	function wpfa_hide_category_widget_list_items( $cat_args ) {

		/** @var array $cat_args - contains current list categories arguments */
		$cat_args = array_merge( $cat_args, array( 'exclude' => apply_filters( 'wpfa_widget_exclude_list', '', 11 ) ) );

		/** Send back the merged array excluding specific category or categories */

		return $cat_args;

	}
	/** End function - hide category widget list items */

	/** Add hide category widget list items */
	add_filter( 'widget_categories_args', array( 'wpfa_hide_category_widget_list_items' );

Enjoy!