• Resolved anUser

    (@anuser)


    I am doing a theme. When admin chooses a services page (page that displays all services) from the existing pages on the site, I want to programmatically insert a block “services” on this page thanks to the id of the page.

    let block = wp.blocks.createBlock( 'core/paragraph', { content: 'test' } );
    wp.data.dispatch( 'core/editor' ).insertBlocks( block );

    The code above is interesting, but that insert a block on the current page, while I wish on another page thanks to its id

Viewing 6 replies - 1 through 6 (of 6 total)
  • Themes shouldn’t be involved in creating blocks. Themes should simply display the content that is already in the page, so that when the user switches themes, the content isn’t affected and creating more content isn’t affected.

    Thread Starter anUser

    (@anuser)

    @@joyously, the theme is linked to a plugin and custom blocks are built into the plugin.

    So the code goes in your plugin, right? Then why mention the theme at all?
    When the user is in the editor for a Page, that’s the only entry that should be changed.
    If the block queries the database for other Pages, then it is a dynamic block and the other Page does not need to change.

    Thread Starter anUser

    (@anuser)

    @joyously

    in a 1st tempas, the admin chooses the services page (among the existing pages). in a second time, a block that indexes all the pages of services that the admin has created must be inserted on this page that was chosen … I have the impression that you do not understand me… if you have a better idea, you can say, I do not have much experience in creating template or plugin

    • This reply was modified 5 years, 5 months ago by anUser.
    • This reply was modified 5 years, 5 months ago by anUser.

    No, I don’t understand you.
    Themes can have a specific template called front-page.php which is used on the Home page URL, whether it is a static Page or a list of Posts. (it has to handle both)
    In that template, it can do what it wants, such as list all the Service pages. You can look at the Twenty Seventeen theme to see how to do that.
    But it is more flexible for the user to just provide a widget area that they can put anything they want in. This is essentially what you get with a static Page because the block editor lets you put blocks of any kind into the Page content. So there is no need for the theme to do anything special for the Home page, and it’s better for the user if the theme doesn’t do anything special, because when they change themes, they will still have their Home page looking like they prefer.
    Meanwhile, you can write a plugin to provide a block that is a list of Pages or a menu or whatever, which is totally separate from the theme. The user can use that block if that’s what they want on their Page. But that block doesn’t need to change any page content. It dynamically gets the list of Pages and outputs them when called, whether it’s in a Page or a Post or wherever.

    Thread Starter anUser

    (@anuser)

    @joyously ,

    function capacity_template() {
      if( $_GET['post'] === capacity_get_option('service', 'services_page')){
        $services_page = get_post_type_object( 'page' );
        $services_page->template = [[ 'capacity/services', [], [] ]];
        $services_page->template_lock = 'all';
        return false;
      }
      [...]
    
    }
    add_action( 'init', 'capacity_template' );

    the above function does what I wish to do … the problem is solved.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Insert block programmatically to a page thanks to its id’ is closed to new replies.