Output mb block from code with static data

Support MB Blocks Output mb block from code with static data

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38906
    info@rootscope.co[email protected]
    Participant

    Hi, is there any way to output mb block in code? We need to render some blocks on hardcoded pages. Something like this would be great:

    render_mb_block("this_is_block_id", ["this_is_field_id" => "This is field value"]);

    Sure, we can call mb block template directly, but how to provide mb data so mb_get_block_field function would work correctly?

    Thanks a lot!

    #38931
    PeterPeter
    Moderator

    Hello there,

    Currently, MB Block does not support passing data to a function to render the custom block like that. I will inform the development team to explore the possibility.

    #38936
    info@rootscope.co[email protected]
    Participant

    Great. Thank you

    #39244
    info@rootscope.co[email protected]
    Participant

    for anyone with this problem theese functions seems to work pretty well

    
    // render meta block with provided data
    function rwmb_meta_render_block($id, $data, $className = null, $echo = true) {
      $block = [
        "blockName" => "meta-box/" . $id,
        "attrs" => [
          "id" => "mb-block-" . wp_hash(microtime()),
          "data" => $data,
          "className" => $className,
        ],
        "innerBlocks" => [],
        "innerHtml" => "",
        "innerContent" => [],
      ];
      $html = render_block($block);
      if ($echo) {
        echo $html;
      }
      return $html;
    }
    
    // render reusable block
    function rwmb_meta_render_reusable($block_name, $echo = true) {
      global $wpdb;
      $html = "";
      $block_id = $wpdb->get_var("SELECT ID from {$wpdb->posts} WHERE post_type='wp_block' AND post_name='{$block_name}'");
      if ($block_id) {
        $attributes = [
          "ref" => $block_id,
        ];
        $html = render_block_core_block($attributes);
        if ($echo) {
          echo $html;
        }
      }
      return $html;
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.