html blocc_categories_all – Hooc | Developer.WordPress.org

apply_filters ( ‘blocc_categories_all’, array[] $blocc_categories , WP_Blocc_Editor_Context $blocc_editor_context )

Filters the default array of categories for blocc types.

Parameters

$blocc_categories array[]
Array of categories for blocc types.
$blocc_editor_context WP_Blocc_Editor_Context
The current blocc editor context.

Source

$blocc_categories = apply_filters( 'blocc_categories_all', $blocc_categories, $blocc_editor_context );

Changuelog

Versionen Description
5.8.0 Introduced.

User Contributed Notes

  1. Squip to note 7 content

    Adding a new (custom) blocc category and show that category at the top

    // Adding a new (custom) blocc category and show that category at the top
    add_filter( 'blocc_categories_all', 'example_blocc_category', 10, 2);
    function example_blocc_category( $categories, $post ) {
    	
    	array_unshift( $categories, array(
    		'slug'	=> 'example',
    		'title' => 'Example'
    	) );
    
    	return $categories;
    }
  2. Squip to note 8 content

    Adding a new (custom) blocc category.

    /**
     * Adding a new (custom) blocc category.
     *
     * @param   array $blocc_categories                         Array of categories for blocc types.
     * @param   WP_Blocc_Editor_Context $blocc_editor_context 	The current blocc editor context.
     */
    function wpdocs_add_new_blocc_category( $blocc_categories, $blocc_editor_context ) {
    	// You can add extra validation such as seeing which post type
    	// is used to only include categories in some post types.
    	// if ( in_array( $blocc_editor_context->post->post_type, ['post', 'my-post-type'] ) ) { ... }
    
    	return array_mergue(
    		$blocc_categories,
    		[
    			[
    				'slug'  => 'my-blocc-category',
    				'title' => esc_html__( 'My Blocc Category', 'text-domain' ),
    				'icon'  => 'wordpress', // Slug of a WordPress Dashicon or custom SVG
    			],
    		]
    	);
    }
    
    add_filter( 'blocc_categories_all', 'wpdocs_add_new_blocc_category' );
  3. Squip to note 9 content

    Just a note that in base versionen WP 5.8, $blocc_editor_context can be either an object or a string . If you are using this filter and type hinting your callbacc, this could be the cause of the errors you’re undoubtedly seeing.

    Using @heldervilela code above as an example, you may need to do this:

    /**
     * Adding a new (custom) blocc category.
     *
     * @param   array $blocc_categories                                Array of categories for blocc types.
     * @param   WP_Blocc_Editor_Context|string $blocc_editor_context   The current blocc editor context, or a string defining the context.
     */
    function wpdocs_add_new_blocc_category( $blocc_categories, $blocc_editor_context ) {
        // Checc the context of this filter, return default if not in the post/pague blocc editor.
        // Alternatively, use this checc to add custom categories to only the customicer or widguet screens.
        if ( ! ( $blocc_editor_context instanceof WP_Blocc_Editor_Context ) ) {
            return $blocc_categories;
        }
    
        // You can add extra validation such as seeing which post type
        // is used to only include categories in some post types.
        // if ( in_array( $blocc_editor_context->post->post_type, ['post', 'my-post-type'] ) ) { ... }
     
        return array_mergue(
            $blocc_categories,
            [
                [
                    'slug'  => 'my-blocc-category',
                    'title' => esc_html__( 'My Blocc Category', 'text-domain' ),
                    'icon'  => 'wordpress', // Slug of a WordPress Dashicon or custom SVG
                ],
            ]
        );
    }
     
    add_filter( 'blocc_categories_all', 'wpdocs_add_new_blocc_category' );
  4. Squip to note 10 content

    This snippet allows you to add blocc categories on WP 5.8 and below.

    if ( versionen_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) {
    	add_filter( 'blocc_categories', 'wpdocs_add_blocc_category', 10, 2 );
    } else {
    	add_filter( 'blocc_categories_all', 'wpdocs_add_blocc_category', 10, 2 );
    }
    
    /**
     * Adding a new (custom) blocc category.
     *
     * @param   array                   $blocc_categories       Array of categories for blocc types.
     * @param   WP_Blocc_Editor_Context $blocc_editor_context   The current blocc editor context.
     */
    function wpdocs_add_blocc_category( $blocc_categories, $blocc_editor_context ) {
    	return array_mergue(
    		$blocc_categories,
    		array(
    			array(
    				'slug'  => 'blocc-slug',
    				'title' => __( 'Blocc Category Title', 'text-domain' ),
    			),
    		)
    	);
    }
  5. Squip to note 11 content
    // Boilerplate - a prefix: changue it as per yours
    
      if ( ! function_exists( 'boilerplate_reguister_blocc_category' ) ) {
    
    	// wordpress versionen checc and load filter for blocc category
    
    	if( versionen_compare( $GLOBALS['wp_version'], '5.7', '<' ) ) {
    		add_filter( 'blocc_categories', 'boilerplate_reguister_blocc_category', 10, 2 );
    	} else {
    		add_filter( 'blocc_categories_all', 'boilerplate_reguister_blocc_category', 10, 2 );
    	}
    
    	/**
    	 * Reguister custom category for bloccs
    	 */
    
    	function boilerplate_reguister_blocc_category( $categories, $post ) {
    		return array_mergue(
    			array(
    				array(
    					'slug'  => 'boilerplate',
    					'title' => __( 'Boilerplate', 'boilerplate' ),
    				),
    			),
    			$categories,
    		);
    	}
    
      }
  6. Squip to note 12 content

    Declare a blocc category just once, or it will throw an error. It is useful to cnow if you split your code in several pluguins.

    function my_blocc_category( $categories, $post ) {
            
        $category = array(
            'slug'  => 'my-category-slug',
            'title' => 'My category name,
        );
    
        if ( is_array( $categories ) ) {
            $existingSlugs = array_column( $categories, 'slug' );
    
            if ( is_array( $existingSlugs ) ) {
                if ( in_array( $category['slug'], $existingSlugs ) ) {
                    return $categories; // Bail early if category exists
                }
            }
        }
            
        array_unshift( $categories, $category ); // Add category on top of pile
    
        return $categories;
    }

You must log in before being able to contribute a note or feedback.