jeudi 10 août 2023

How to add categories to WordPress block patterns?

I'm trying to create categories for WP block patterns. So far I have:

// Custom taxonomy for wp_block post type
function register_patterns_taxonomy() {
    $labels = array(
        'name' => 'Pattern category',
        'singular_name' => 'Pattern category',
        'search_items' => 'Search Patterns categories',
        'all_items' => 'All Patterns categories',
        'parent_item' => 'Parent Pattern category',
        'parent_item_colon' => 'Parent Pattern category:',
        'edit_item' => 'Edit Pattern category',
        'update_item' => 'Update Pattern category',
        'add_new_item' => 'Add New Pattern category',
        'new_item_name' => 'New Pattern category Name',
        'menu_name' => 'Patterns categories',
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'show_in_rest' => true,
        'rewrite' => array( 'slug' => 'patterns_categories' ),
    );

    register_taxonomy( 'patterns_categories', array( 'wp_block' ), $args );
}
add_action( 'init', 'register_patterns_taxonomy' );

// creating patters categories by taxonomy terms
function create_block_pattern_category_for_terms() {
    $taxonomy = 'patterns_categories'; // Zmień na nazwę swojej taksonomii
    $terms = get_terms( array(
        'taxonomy' => $taxonomy,
        'hide_empty' => false, // Pobierz wszystkie termy, także te puste
    ) );

    foreach ( $terms as $term ) {
        $pattern_category = sanitize_title( $term->name ); // Tworzenie unikalnej nazwy kategorii
        $pattern_category_exists = get_terms( 'patterns_categories', array( 'slug' => $pattern_category ) );

        register_block_pattern_category(
            $term->slug,
            array( 'label' => __( $term->name, 'wpdocs-my-plugin' ) )
        );

    }
}
add_action( 'init', 'create_block_pattern_category_for_terms' );

and that works fine, I have custom taxonomy visible in block pattern post, also I successfully created blocks pattern categories based on custom taxonomy terms.

But now, I need to add somehow these terms to blocks patterns.

My idea is using register_block_pattern() for each post in wp_block post type, and add properly: title, content and categories. So I've tried:

// trying to create patterns
$args = array(
    'post_type' => 'wp_block',
    'posts_per_page' => -1,
);

$posts = get_posts($args);

foreach ($posts as $post) {
    setup_postdata($post);

    $title = get_the_title();
    $content = get_the_content();

    // Fetch terms associated with this post
    $terms = wp_get_post_terms($post->ID, 'patterns_categories', array('fields' => 'slugs'));

    // Create block pattern
    register_block_pattern(
        'handy-options/' . $title,
        array(
            'title'       => $title,
            'description' => _x('Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'theme-slug'),
            'categories'  => $terms,
            'content'     => wp_json_encode($content),
        )
    );
}
wp_reset_postdata(); 

but I don't know is this a good way - although new block patters are created with correct title, I can't add terms and content correctly.

Any advice, idea?

Aucun commentaire:

Enregistrer un commentaire