justintadlock / block-pattern-builder

Build block patterns from the WordPress admin.
GNU General Public License v2.0
42 stars 9 forks source link

Add pattern categories #17

Closed ajitbohra closed 1 year ago

ajitbohra commented 4 years ago

closes #11

justintadlock commented 4 years ago

Mostly looks good. There's some things we need to do on the pattern registration end of things too.

ajitbohra commented 4 years ago

yes working on missing parts, PR was not fully ready.

ajitbohra commented 4 years ago

@justintadlock

PR now register taxonomy and pattern categories.

justintadlock commented 4 years ago

I think this looks pretty good with a quick scan. I'll want to go over it a bit more with testing, etc. since it's a bigger change. Thanks for the work on this!

ghost commented 3 years ago

Looking forward to this feature being released.

As a temporary workaround, you can remove theme support for block patterns, register a single placeholder pattern and category, and then use the Block Pattern plugin to manage your custom patterns. All of the patterns created with the plugin will fall in a "non-category" of Uncategorized. We tried unregistering everything and just registering a placeholder category to see if only the Uncategorized patterns would be displayed. But WordPress still shows the category. We also tried naming a category of "Z Default" to see if the Uncategorized would be the default selected category in the pattern picker, but Uncategorized always sorts to the very bottom of the category picker dropdown. What we plan to do until the plugin supports categories is create a simple default category and pattern. Then we'll prefix all of our custom patterns. That way our users can search for them easily. Not as elegant as we'd like, but more workable than site editors continually forgetting to convert reusable blocks to regular blocks before editing them and nuking the parent reusable block (it's happened several times in the past week because Gutenburg changed the interface to convert reusable blocks).

// Unregister WordPress Core default block patterns
add_action( 'after_setup_theme', 'cob_remove_patterns' );
function cob_remove_patterns() {
   remove_theme_support( 'core-block-patterns' );
}

//Add a default block pattern to address a bug in the Block Pattern plugin until it starts supporting categories

function my_register_block_patterns()
  {
    if (class_exists('WP_Block_Patterns_Registry')) {
      // register a placeholder default pattern
      register_block_pattern('mine/z-Default', [
        'title' => __('Z Default', 'textdomain'),
        'description' => _x(
          'Default block pattern',
          'Block pattern description',
          'textdomain'
        ),
        'content' =>
          "default",
        'categories' => ['Z Default'],
      ]);

      // register placeholder category
      register_block_pattern_category('Z Default', [
        'label' => _x('Z Default', 'textdomain'),
      ]);
    }
  }

add_action( 'init', 'my_register_block_patterns' );