Support » Developing with WordPress » Reorder Editor Inserter Blocks

  • Resolved shane502

    (@shane502)


    I am trying to put my custom block category on top of the block inserter list, but I cannot get it on top, even by adding an ‘order’ key to my block category array. Here is my category code and filter hook I am using (please note my hook is in a site class constructor function, so it looks different). Is there another hook or action I can use to modify which category is first in the list? PHP answer preferred, I can try to hobble together some js if absolutely necessary – it is something I am definitely still learning :).

    If there is something I can add or edit in my function that would be helpful. I have searched around and all I have managed to find is how to control blocks, but not the inserter order.

    
    add_filter( 'block_categories', [ $this, 'kwa_block_category' ], 10, 2 );
    
    // create a custom category for our theme-specific blocks
    function kwa_block_category( $categories, $post ) {
        return array_merge( $categories,
        [[
            'slug' => 'kwa-blocks',
            'title' => 'KWA Blocks'
        ], ]);
    }
    

    *Edit: here is what it looks like currently
    https://pasteboard.co/IXzcBR0.png

    • This topic was modified 10 months, 2 weeks ago by shane502.
    • This topic was modified 10 months, 2 weeks ago by shane502.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @shane502,

    You will need to create a new array of categories. Then add the desired category first and then append the new array with original category blocks. Also, change the priority of the filter to 99 so that additional gutenberg blocks plugin’s categories will be added at the bottom instead of getting at top.

    Please refer my code snippet below:

    
    function checkCategoryOrder($categories)
    {
        //custom category array
    	$temp = array(
            'slug'  => 'testing-blk',
            'title' => 'Testing Blocks'
        );
        //new categories array and adding new custom category at first location
        $newCategories = array();
        $newCategories[0] = $temp;
    
        //appending original categories in the new array
        foreach ($categories as $category) {
            $newCategories[] = $category;
        }
    
        //return new categories
        return $newCategories;
    }
    add_filter( 'block_categories', 'checkCategoryOrder', 99, 1);
    

    Please let me know your response on this!

    Hey! That did the trick! I was starting to think modifying the order of the existing inserter items was not possible. I also realized I had $post in my original call for some reason :facepalm: So I was also able to remove that as well after I noticed you only had 1 expected response in the filter hook.

    Thank You!

    Here is the code I used for posterity (I only modified the naming convention and array style to my preference – in case anyone wants to know what my implementation was).

    
    add_filter( 'block_categories', [ $this, 'kwa_block_category' ], 99, 1 );
    
    function kwa_block_category( $categories ) {
        // setup category array
        $kwa_category = [
    	'slug' => 'kwa-blocks',
    	'title' => 'KWA Blocks'
        ];
    
        // make a new category array and insert ours at position 1
        $new_categories = [];
        $new_categories[0] = $kwa_category;
    
        // rebuild cats array
        foreach( $categories as $category ) {
            $new_categories[] = $category;
        }
    
        return $new_categories;
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Reorder Editor Inserter Blocks’ is closed to new replies.