skip to Main Content

I want to keep my custom category first.Someone can help.

See this my Code,

function add_elementor_widget_categories( $elements_manager ) {

    $elements_manager->add_category(
        'custom-widget',
        [
            'title' => __( 'Custom Widget', 'elementor' ),
            'icon' => 'fa fa-plug',
            'active' => true,
        ]
    );

}
add_action( 'elementor/elements/categories_registered', 'add_elementor_widget_categories' );

2

Answers


  1. I use something like this

    add_action( 'elementor/elements/categories_registered', function(ElementorElements_Manager $elements_manager ) {
    
            //add our categories
            $category_prefix = 'aqb-';
    
            $elements_manager->add_category(
                $category_prefix . 'widgets',
                [
                    'title' => 'Blah Blah',
                    'icon' => 'fa fa-plug',
                ]
            );
    
            //hack into the private $categories member and reorder it so our stuff is at the top
    
            $reorder_cats = function() use($category_prefix){
                uksort($this->categories, function($keyOne, $keyTwo) use($category_prefix){
                    if(substr($keyOne, 0, 4) == $category_prefix){
                        return -1;
                    }
                    if(substr($keyTwo, 0, 4) == $category_prefix){
                        return 1;
                    }
                    return 0;
                });
    
            };
            $reorder_cats->call($elements_manager);
    
        } );
    

    Based on this https://github.com/elementor/elementor/issues/7445#issuecomment-472822406

    Login or Signup to reply.
  2. The solution that worked for me was

    function add_elementor_widget_categories( $elements_manager ) {
    
        $categories = [];
        $categories['NEWCAT'] =
            [
                'title' => 'NEWCAT',
                'icon'  => 'fa fa-plug'
            ];
    
        $old_categories = $elements_manager->get_categories();
        $categories = array_merge($categories, $old_categories);
    
        $set_categories = function ( $categories ) {
            $this->categories = $categories;
        };
    
        $set_categories->call( $elements_manager, $categories );
    
    }
    
    add_action( 'elementor/elements/categories_registered', 'add_elementor_widget_categories') ;
    

    Also found on the comment thread from above: https://github.com/elementor/elementor/issues/7445#issuecomment-692123467

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search