Title: plugin creating a reusable block
Last modified: December 12, 2020

---

# plugin creating a reusable block

 *  [pgn4web](https://wordpress.org/support/users/pgn4web/)
 * (@pgn4web)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/plugin-creating-a-reusable-block/)
 * Hello,
 * my plugin creates a reusable block following the suggestion in this support thread:
   
   [https://wordpress.org/support/topic/how-to-instruct-a-plugin-to-create-a-reusable-block/](https://wordpress.org/support/topic/how-to-instruct-a-plugin-to-create-a-reusable-block/)
 * It all works fine as long as I’m logged-in as the wordpress administrator, however
   when logged-in as an author I can see the reusable block in the list, but it 
   fails to display properly.
 * The issue seems to relate to the code checking whether the reusable block is 
   already present:
 *     ```
       $my_shortcode_name = 'my short code';
       if ( !get_posts( array( 'name' => sanitize_title( $my_shortcode_name ), 'post_type' => 'wp_block', 'posts_per_page' => 1 ) ) ) {
       ```
   
 * If I remove the check above, the reusable block works properly (but I see many
   instances in the block selector).
 * Any reason why the code above would work properly for the site administrator 
   but not work for an author?

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

 *  Thread Starter [pgn4web](https://wordpress.org/support/users/pgn4web/)
 * (@pgn4web)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/plugin-creating-a-reusable-block/#post-13781472)
 * Maybe just a wild guess… when the plugin is installed by the site administrator
   a reusable block is created since none is present.
 * Then when the author user accesses the system, it can see the reusable block 
   created by the administrator, but for some reason I can not understand, the author
   user can not load properly the reusable block created by the administrator user.
 * How do I get around this?
 * This is my full code to create the reusable block:
 *     ```
           $my_shortcode_name = 'my short code';
           if ( !get_posts( array( 'name' => sanitize_title( $my_shortcode_name ), 'post_type' => 'wp_block', 'posts_per_page' => 1 ) ) ) {
             wp_insert_post( array(
               'post_content'   => "<!-- wp:shortcode -->\n[pgn] { click on the block options menu and convert to regular block before editing the PGN data } [/pgn]\n<!-- /wp:shortcode -->",
               'post_title'     => $my_shortcode_name,
               'post_type'      => 'wp_block',
               'post_status'    => 'publish',
               'comment_status' => 'closed',
               'ping_status'    => 'closed',
               'guid'           => sprintf( '%s/wp_block/%s', site_url(), sanitize_title( $my_shortcode_name ) )
             ) );
           }
       ```
   
 *  Thread Starter [pgn4web](https://wordpress.org/support/users/pgn4web/)
 * (@pgn4web)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/plugin-creating-a-reusable-block/#post-13781577)
 * A bit more debugging and it really seems that an author user is NOT able to insert
   properly a reusable block created by the admin user when installing a plugin 
   containing the following code (despite seeing the reusable block in the list):
 *     ```
             wp_insert_post( array(
               'post_content'   => "<!-- wp:shortcode -->\n[pgn] { click on the block options menu and convert to regular block before editing the PGN data } [/pgn]\n<!-- /wp:shortcode -->",
               'post_title'     => $my_shortcode_name,
               'post_type'      => 'wp_block',
               'post_status'    => 'publish',
               'comment_status' => 'closed',
               'ping_status'    => 'closed',
               'guid'           => sprintf( '%s/wp_block/%s', site_url(), sanitize_title( $my_shortcode_name ) )
             )
       ```
   
 * Any suggestion how to make the reusable block created programmatically by the
   plugin fully accessible by all users?

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

The topic ‘plugin creating a reusable block’ is closed to new replies.

 * ![](https://ps.w.org/gutenberg/assets/icon-256x256.jpg?rev=1776042)
 * [Gutenberg](https://wordpress.org/plugins/gutenberg/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/gutenberg/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/gutenberg/)
 * [Active Topics](https://wordpress.org/support/plugin/gutenberg/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/gutenberg/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/gutenberg/reviews/)

 * 2 replies
 * 1 participant
 * Last reply from: [pgn4web](https://wordpress.org/support/users/pgn4web/)
 * Last activity: [5 years, 5 months ago](https://wordpress.org/support/topic/plugin-creating-a-reusable-block/#post-13781577)
 * Status: not resolved