• Hello,

    my plugin creates a reusable block following the suggestion in this support thread:
    https://www.ads-software.com/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

    (@pgn4web)

    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

    (@pgn4web)

    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.