Home>Support>Use the Layout builder on all posts

Use the Layout builder on all posts

Hello,

I’m using the page builder for a new project and it’s always a thrill to use.
However, in this case the designer has come up with an idea i’m not really a fan of, but i have to solve it anyway. Business as usual. I’ve ended up realizing that some post types should have individual headers, specified on each post.
The content of this header will be quite dynamic. My approach is therefore to have a metabox with a button to the Layout builder widget. That is, it should work as if i had a sidebar and added this widget to it.

Any suggestions of how to achieve that? Registering sidebars and in my metabox select a sidebar will not be an acceptable acceptable solution, since there will be quite many sidebars.

Another solution would be to add another wp_editor() to my metabox, but how do i add the tab for siteorigin here?

This is our free support forum. Replies can take several days. If you need fast email support, please purchase a SiteOrigin Premium license.

  1. 6 years, 17 days ago Linuzitac

    Ok, i was able to solve the problem. For the one interested in how, this is what i did;

    • In my metabox class i extended WP_Widget
    • In my metabox class i added a function called form, takin parameter $instance (copied from https://github.com/siteorigin/siteorigin-panels/blob/develop/inc/widgets/layout.php#L74)
    • in my metabox save i added
      $sow_data = array(
                  'panels_data' => $_POST['widget-'][0]['panels_data'],
                  'builder_id' => $_POST['widget-'][1]['builder_id']
              );

      And saves this with

      update_post_meta( $post_id, 'post-header-sow', $sow_data );
    • in my metabox render function i use this code for calling the form function;
      $instance = get_post_meta($post->ID, 'post-header-sow');
              $this->form($instance[0]);

    Please come back with better/other soluitons.

  2. 6 years, 17 days ago Linuzitac

    And this leads me to question no 2. Now i can admin the content correctly. But how do use this data to render? In my themes header.php i want to render my json string.

    This is what i have:

    Array
    (
        [0] => Array
            (
                [panels_data] => {"widgets":[{"title":"","text":"Just some test....","text_selected_editor":"tinymce","autop":"on","_sow_form_id":"163005b8e4ab3c1d7b298904274","_sow_form_timestamp":"1536053120772","panels_info":{"class":"SiteOrigin_Widget_Editor_Widget","raw":true,"grid":0,"cell":0,"id":0,"widget_id":"58835b0b-86fa-4b99-9b2c-55658cd0e632","style":{"id":"","class":"","widget_css":"","mobile_css":"","margin":"","padding":"","mobile_padding":"","background":"","background_image_attachment":"0","background_image_attachment_fallback":"","background_display":"tile","border_color":"","font_color":"","link_color":""}}}],"grids":[{"cells":1,"style":{}}],"grid_cells":[{"grid":0,"index":0,"weight":1,"style":{}}]}
                [builder_id] => 5b8e4aa9c0786
            )
    
    )

    the

    SiteOrigin_Panels::renderer()->render()

    takes the post id for checkign the post meta ‘panel data’, but i want to use this function with my stored data above.

    Should be a simple task?

  3. 6 years, 11 days ago Alex S
    Hi, I Work Here

    Hi Linuzitac,

    I’m glad to hear you were able to work with Metabox as desired. I’m sorry I wasn’t able to be of assistance with doing that. I don’t have any suggestions on general improvements – what you’ve done looks fine.

    Regarding your second post, it’s possible to pass data using the mentioned method by passing the data through the $panels_data parameter. As an example, you could do something like this:

    SiteOrigin_Panels::renderer()->render( false, true, $panels_data );
    
  4. 6 years, 11 days ago Linuzitac

    Hi Alex, and thank you.

    The data i have saved is this:

    Array
    (
        [0] => Array
            (
                [panels_data] => {"widgets":[{"title":"","text":"This is just a test!!","text_selected_editor":"html","autop":"on","_sow_form_id":"127415b8fe698c987b040657557","_sow_form_timestamp":"1536157345616","panels_info":{"class":"SiteOrigin_Widget_Editor_Widget","raw":true,"grid":0,"cell":0,"id":0,"widget_id":"c6f3707c-d4ab-4722-a716-b38c10061b69","style":{"id":"","class":"","widget_css":"","mobile_css":"","margin":"","padding":"","mobile_padding":"","background":"","background_image_attachment":"0","background_image_attachment_fallback":"","background_display":"tile","border_color":"","font_color":"","link_color":""}}}],"grids":[{"cells":1,"style":{}}],"grid_cells":[{"grid":0,"index":0,"weight":1,"style":{}}]}
                [builder_id] => 5b8e4aa9c0786
            )
    
    )

    Lets say I got this in a variable called $a.
    I have tried:

    SiteOrigin_Panels::renderer()->render( false, true, $a );
    SiteOrigin_Panels::renderer()->render( false, true, $a[0] );
    SiteOrigin_Panels::renderer()->render( false, true, $a[0]['panels_data'] );
    

    All of them gives me an empty string.
    Any data i need to save that I’ve missed or something?

  5. 6 years, 11 days ago Alex S
    Hi, I Work Here

    Hi Linuzitac,

    Can you please provide more information on how $a was set? Did you fetch the data and then pass it or did you do any processing before hand?

  6. 6 years, 11 days ago Linuzitac

    $a in this case is just metadata I’ve saved for the post.
    I have a metabox for handling the post header with a SOW layoutbuilder.

    As i tried to describe in the post before, this is what i have;

    • a metabox class in which i extend WP_Widget (i can try to extend SiteOrigin_Widget instead).
    • when the post is saved i fetch these two values with the code
      $sow_data = array(
                  'panels_data' => $_POST['widget-'][0]['panels_data'],
                  'builder_id' => $_POST['widget-'][1]['builder_id']
              );
              update_post_meta( $post_id, 'post-header-sow', $sow_data );
              

    This is enough data to handle my layout builder in the metabox (with some js ans stuff).

    In the theme then get my $a by

     get_post_meta( get_the_id(), 'post-header-sow'); 

    I realized that i’m passing a json string to the

    SiteOrigin_Panels::renderer()->render();

    When i try

    SiteOrigin_Panels::renderer()->render(false, true, json_decode($a[0]['panels_data']));

    i get

    Fatal error: Uncaught Error: Cannot use object of type stdClass as array in C:\wamp\www\mysite\wp-content\plugins\siteorigin-panels\siteorigin-panels.php on line 459

    and

    Error: Cannot use object of type stdClass as array in C:\wamp\www\mysite\wp-content\plugins\siteorigin-panels\siteorigin-panels.php on line 459
  7. 6 years, 11 days ago Linuzitac

    Aaaaand that error is because i have json_decode with default false, if i do

    SiteOrigin_Panels::renderer()->render(false, true, json_decode($a[0]['panels_data'], true));

    it converts to arrays instead of stdClass. This gives me no errors, but still an empty string. :)

  8. 6 years, 11 days ago Linuzitac

    Aaaaaaand it works when i echo it… Sorry :)

    echo SiteOrigin_Panels::renderer()->render(false, true, json_decode($a[0]['panels_data'], true));
Replies on this thread are closed. Please create a new thread if you have a question, or purchase a SiteOrigin Premium license if you need one-on-one email support.

Get The Most Out of SiteOrigin with SiteOrigin Premium

Find Out More