Title: How do you insert patterns programmatically?
Last modified: September 9, 2024

---

# How do you insert patterns programmatically?

 *  [josuebdigital](https://wordpress.org/support/users/josuebdigital/)
 * (@josuebdigital)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/how-do-you-insert-patterns-programmatically/)
 * 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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fhow-do-you-insert-patterns-programmatically%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [technocrackers](https://wordpress.org/support/users/technocrackers/)
 * (@technocrackers)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/how-do-you-insert-patterns-programmatically/#post-18003253)
 * 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
 *     ```wp-block-code
       // Register the shortcodefunction 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 shortcodeadd_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:
 *     ```wp-block-code
       [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](https://wordpress.org/support/users/josuebdigital/)
 * (@josuebdigital)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/how-do-you-insert-patterns-programmatically/#post-18003354)
 * 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?
 *  [technocrackers](https://wordpress.org/support/users/technocrackers/)
 * (@technocrackers)
 * [1 year, 8 months ago](https://wordpress.org/support/topic/how-do-you-insert-patterns-programmatically/#post-18003873)
 * 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.
 *     ```wp-block-code
       // Register the shortcodefunction 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 shortcodeadd_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:
 *     ```wp-block-code
       [insert_pattern]
       ```
   
 *  [delanthear](https://wordpress.org/support/users/delanthear/)
 * (@delanthear)
 * [1 year, 6 months ago](https://wordpress.org/support/topic/how-do-you-insert-patterns-programmatically/#post-18162173)
 * 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)

The topic ‘How do you insert patterns programmatically?’ is closed to new replies.

## Tags

 * [WordPress](https://wordpress.org/support/topic-tag/wordpress/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 3 participants
 * Last reply from: [delanthear](https://wordpress.org/support/users/delanthear/)
 * Last activity: [1 year, 6 months ago](https://wordpress.org/support/topic/how-do-you-insert-patterns-programmatically/#post-18162173)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
