Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HelpHub] Feedback on Block Patterns #1696

Open
zzap opened this issue Oct 4, 2024 · 5 comments
Open

[HelpHub] Feedback on Block Patterns #1696

zzap opened this issue Oct 4, 2024 · 5 comments
Assignees
Labels
HelpHub feedback Issues reported via feedback form at HelpHub self-assigned [Status] Review Issue in review [Status] To do Issue marked as Todo user documentation (HelpHub) Improvements or additions to end-user documentation

Comments

@zzap
Copy link
Member

zzap commented Oct 4, 2024

URL of the Page with the Issue

https://wordpress.org/documentation/article/block-pattern/

Originally reported by

https://profiles.wordpress.org/brnggncy/

Report content (issue description)

Is there a way to limit patterns to specific post types?

Section of Page with the issue

Why is this a problem?

Suggested Fix

@zzap zzap added [Status] To do Issue marked as Todo HelpHub feedback Issues reported via feedback form at HelpHub user documentation (HelpHub) Improvements or additions to end-user documentation labels Oct 4, 2024
Copy link

github-actions bot commented Oct 4, 2024

Heads up @WordPress/docs-issues-coordinators, we have a new issue open. Time to use 'em labels.

@karthick-murugan
Copy link
Collaborator

/assign

Copy link

Hey @karthick-murugan, thanks for your interest in this issue! 🍪🍪🍪
If you have any questions, do not hesitate to ask them in our #docs Slack channel.
Enjoy and happy contributing ❤️

@karthick-murugan
Copy link
Collaborator

Step 1: Add This Code to Your Theme’s functions.php File

Copy and paste the following code into your theme’s functions.php file or a custom plugin:

function mytheme_limit_pattern_to_post_types() {
    // Define the post types where the pattern should appear
    $allowed_post_types = array( 'post', 'page' ); // Change these to your desired post types

    // Get the current post type
    $current_post_type = get_post_type();

    // Check if the current post type is allowed
    if ( in_array( $current_post_type, $allowed_post_types, true ) ) {
        // Register the pattern only for allowed post types
        register_block_pattern(
            'mytheme/custom-pattern',
            array(
                'title'       => __( 'Custom Pattern', 'mytheme' ),
                'description' => __( 'A custom pattern for specific post types.', 'mytheme' ),
                'content'     => '<!-- wp:paragraph --><p>This is a custom pattern limited to specific post types.</p><!-- /wp:paragraph -->',
                'categories'  => array( 'text' ),
            )
        );
    }
}
add_action( 'init', 'mytheme_limit_pattern_to_post_types' );

Step 2: Customize the Code

  1. Change $allowed_post_types:

Replace 'post', 'page' with the post types where you want the pattern to appear (e.g., 'product' for WooCommerce products).

  1. Change the Pattern Content:

Replace the content with your desired block pattern code.

@karthick-murugan karthick-murugan added the [Status] Review Issue in review label Feb 5, 2025
Copy link

github-actions bot commented Feb 5, 2025

Heads up @docs-reviewers - the "[Status] Review" label was applied to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HelpHub feedback Issues reported via feedback form at HelpHub self-assigned [Status] Review Issue in review [Status] To do Issue marked as Todo user documentation (HelpHub) Improvements or additions to end-user documentation
Projects
Status: No status
Development

No branches or pull requests

2 participants