• anniew86

    (@anniew86)


    Hello folks,

    I’m recreating old site with wordpress. The old site has many pages generated from structured content, the pages consist of a mixture of headings, paragraphs and images in various order. I want to create one page in wordpress and generate the content through plugin. In this plugin I would like to generate standard core blocks, they should be correct rendered on frontend through theme, but there is no need to edit them or store in database. So the question is about the recommended way to insert blocks on fly to the page. My idea was to do it in the filter ‘the_content’ and output the blocks through serialize_block() as described in the comment to this function on in the reference doc, something like:

      ...
      $block_name = 'core/paragraph';
      $innerHTML  = 'Sample paragraph text.';
      $converted_block = new WP_Block_Parser_Block( $block_name, array(), array(), 
      $innerHTML, array( $innerHTML ) );
      $serialized_block = serialize_block( (array) $converted_block );
      echo $serialized_block;
      ...

    My questions, is it the recomended way to create blocks on the fly?
    Will the blocks have the needed css classes?
    Will they be correctly rendered and displayed through themes?

    Regards,
    Annie

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The simplest way to dynamically create blocks is to directly insert a block’s normal HTML code into post content. For each block type you’d want to use, examine the saved HTML code of an existing block. Emulate the exact same structure for your actual content. If your inserted HTML looks like a block, it’ll get classes like a block.

    The easiest way to make existing HTML content into a block is to make the entire, existing HTML into one big custom HTML block. If it’s not going to be edited, there’s not much point in breaking it down into individual discrete blocks. Of course, you’d also need to retain the existing CSS. Then adding classes and taking on theme styling wouldn’t even be applicable. Maybe not your intention, but it is the easiest way to import existing content. If you want to make things more difficult, it’s your choice to make ??

    Thread Starter anniew86

    (@anniew86)

    thanks al lot for your reply. I don’t look for the most quict & dirty solution. Is there no suitable api for this? Direct inserts of html tags doesn’t seem to be future proof for me. How wordpress inserts the core objects?

    Moderator bcworkz

    (@bcworkz)

    You can generate content in any manner you wish through an external app, then save it into WP through its REST API.

    Inserting HTML tags wouldn’t become an issue in the future. Any future compatibility issue would come from the block comment formatting, in particular what attribute values might be saved with them. If a block you add ends up becoming incompatible, it will at least fail gracefully since it’s just a comment after all.

    In any case, I don’t even see block attributes becoming incompatible because altering acceptable attributes will cause issues for everyone who used the same blocks, whether created quick and dirty or properly through the editor.

    WP saves objects we create by directly querying the DB through various PHP functions defined for the purpose, such as wp_insert_post(). The block editor saves post data through the above linked API. The API server also uses defined functions like wp_insert_post() to manipulate DB data.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creation of blocks from PHP’ is closed to new replies.