• Can you insert an existing pattern in a page via PHP? Sometimes, patterns slows down (or directly crashes) my editor, and I am currently trying to write a code to insert my patterns via shortcodes so they do not load while editing.

    Is it possible?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Yes, it is possible to insert an existing pattern in a WordPress page via PHP and shortcodes. This approach can help prevent your editor from slowing down or crashing by deferring the loading of the pattern until it’s rendered on the front end, rather than during the editing process.

    1. Register a Shortcode in WordPress with below code

    // Register the shortcode
    function insert_pattern_via_shortcode() {
    // Check if the function
    do_blocks() exists
    if ( function_exists( 'do_blocks' ) ) {
    // Replace 'my-pattern-slug' with your pattern's slug
    $pattern = get_block_pattern( 'my-pattern-slug' );

    if ( $pattern ) {
    return do_blocks( $pattern['content'] );
    }
    }
    return '';
    }

    // Add the shortcode
    add_shortcode( 'insert_pattern', 'insert_pattern_via_shortcode' );

    2. Using the Shortcode in Pages/Posts
    Once the shortcode is registered, you can use it in your pages or posts to insert the desired pattern. Simply add the following shortcode where you want the pattern to appear:

    [insert_pattern]

    How it Works:

    • The shortcode defers the pattern loading until the page is rendered on the front end.
    • The?get_block_pattern()?function retrieves the content of the block pattern by its slug (replace?'my-pattern-slug'?with the actual slug of the pattern you want to insert).
    • The?do_blocks()?function processes the pattern’s block content, which ensures it is properly rendered.
    Thread Starter josuebdigital

    (@josuebdigital)

    Thank you.

    Unfortunately, the code does not work. Seems like WordPress cannot find any function named “get_block_pattern”:

    Fatal error: Uncaught Error: Call to undefined function get_block_pattern()

    Do you know how to fix this?

    In WordPress, block patterns are registered and accessed via WP_Block_Patterns_Registry. Instead of get_block_pattern(), you should use WP_Block_Patterns_Registry::get_instance()->get_registered() to retrieve the block pattern.

    // Register the shortcode
    function insert_pattern_via_shortcode() {
    // Ensure the block patterns registry exists
    if ( class_exists( 'WP_Block_Patterns_Registry' ) ) {
    // Replace 'my-pattern-slug' with your pattern's slug
    $pattern = WP_Block_Patterns_Registry::get_instance()->get_registered( 'my-pattern-slug' );

    if ( $pattern && isset( $pattern['content'] ) ) {
    return do_blocks( $pattern['content'] );
    }
    }
    return '';
    }

    // Add the shortcode
    add_shortcode( 'insert_pattern', 'insert_pattern_via_shortcode' );

    Explanation:

    • WP_Block_Patterns_Registry::get_instance()->get_registered( 'my-pattern-slug' ): This retrieves the registered pattern by its slug.
    • $pattern['content']: Accesses the content of the block pattern if it exists.
    • do_blocks( $pattern['content'] ): Processes the pattern’s block content and outputs it.

    How to Use:

    1. Replace 'my-pattern-slug' with the slug of the block pattern you want to insert. You can find the slug in your block pattern registration code.
    2. Add the [insert_pattern] shortcode in any post or page to display the pattern.

    Usage Example in Pages/Posts:

    [insert_pattern]

    There doesn’t appear to be any way to find the slug of a pattern in the wordpress admin screen anymore. Any ideas where you can get it?

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.