Custom Conditions

Knowledgebase Docs » UberMenu Conditionals
USEFUL? 9
UberMenu 3

If you need a conditional statement more advanced than the provided parameters, UberMenu Conditionals allows you to write a custom conditional filter.

Please note this is an advanced tutorial and requires PHP and WordPress filter knowledge. This article provides guidance but these methods are considered customizations and are the customer’s responsibility

The best place to put custom conditional filters would be in your child theme’s functions.php, or in a separate plugin. This is totally up to you and depends on how you manage your site.

Registering Custom Conditions

Registering a custom condition allows you to create new conditions which will appear in the UberMenu Menu Item Conditionals settings options.

Here’s an example of a Condition, which checks if the user is on the front page and has permission to view lessons (an example capability that you might have if you’re selling a course)

//Register the custom Conditional option
add_filter( 'ubermenu_conditionals_options' , 'register_my_custom_condition_option' );
function register_my_custom_condition_option( $ops ){
	if( !isset( $ops['custom'] ) ) $ops['custom'] = array();
	$ops['custom']['my_condition'] = array(			//my_condition is used to identify this condition
		'name'	=> 'My Custom Condition',	//This name will appear in the settings panel
		'desc'	=> 'This is super cool.'	//The condition's description
	);
	return $ops;
}

//Add our custom conditional test
add_filter( 'ubermenu_conditionals_custom_condition' , 'my_custom_condition_test' , 10 , 3 );
function my_custom_condition_test( $display_on, $condition , $param ){

	//Only apply for this specific condition
	if( $condition == 'my_condition' ){

		//Test actual condition
		if( is_front_page() && current_user_can( 'view_lessons' ) ){
			//Adjust display_on appropriately
			$display_on = true;
		}
		else $display_on = false;

	}

	//Always return $display_on
	return $display_on;
}

Direct Filter

Using a direct filter will mean you control the display of the menu item via PHP, bypassing the UberMenu Menu Item Conditionals settings.

Here’s an example of a custom conditional filter that makes sure a particular menu item will not display on the front page for logged in users.

function my_custom_conditional_filter( $display_on , $walker , $element , $max_depth, $depth, $args ){
 
    //The ID of the menu item currently being filtered
    $id = $element->ID;
 
    //Check for that specific menu item
    if( $id == 268 ){
 
        //If we're currently logged in AND on the front page
        if( is_user_logged_in() && is_front_page() ){
 
            //Disable the menu item
            $display_on = false;
 
        }
 
    }
 
    //Return the filtered display boolean
    return $display_on;
}
add_filter( 'ubermenu_display_item', 'my_custom_conditional_filter', 20, 6 );