UberMenu bug? fix - add custom class to an ubermenu item

abda53

Member
Jun 10, 2022
84
92
18
On the latest version of UberMenu, there is a bug where the Custom Content -> Custom Class value is not shown on the site. In the code, the custom class value is tied in with custom content (It will only show if custom content is used). There is a spot to enter a custom class under General -> Anchor Class, but that only applies the class to the a tag, not the parent (or sub menus)

I don't know if it's an actual bug because maybe it was intended this way.. however it's not working how I needed it to (I needed a menu class on specific menus to be applied to the block li tag - without custom content.

Here is how to use the Custom Class independently from Custom Content.

/plugins/ubermenu/includes/menuitems/UberMenuItem.class.php

find: function add_class_layout_columns(){

add above it
PHP:
function get_custom_class(){
    $custom_class = $this->getSetting( 'custom_content_class' );
    if( $custom_class ){
        return ' ' . sanitize_html_class( $custom_class );
    }
    else{
        return false;
    }
}

add inside function add_class_layout_columns(), above $cols = $this->getSetting( 'columns' )
PHP:
$this->item_classes[] = $this->get_custom_class();

so it should look something like this
PHP:
    function get_custom_class(){
        $custom_class = $this->getSetting( 'custom_content_class' );
        if( $custom_class ){
            return ' ' . sanitize_html_class( $custom_class );
        }
        else{
            return false;
        }
    }  

    function add_class_layout_columns(){

        if( $this->depth > 0 ){
            $parent_submenu_type = $this->walker->parent_item()->getSetting('submenu_type_calc');
            if( $parent_submenu_type == 'flyout' ) return;    //no columns in flyouts
        }
      
        $this->item_classes[] = $this->get_custom_class();

This works, however it's not ideal since any plugin updates will remove it. It will be better extend the UberMenuItem class (if possible). I haven't spent time on that, but if I do I will update the code here
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu