This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Tagged: ,

Creating a new custom layout programmatically, possible with Layout Builder API?

  • Creator
    Topic
  • #5802
    srumery
    Participant

    Can we create a new builder layout in a child theme functions file using the Layout Builder API? Here is a bit more detail on my question…

    I just finished creating a function that is fired off when the theme is activated. It’s job is to setup default pages, a new menu with a few menu items as well as several other time saving tasks. I would like to do the same thing with the Builder. I have a default layout that I will be using for this child theme and I would like to automate it.

    My layout consists of five “Content” areas, each separated by a “Divider”. All “Content” areas use “Content from external page” for the “Content Source” and the external page is created in the function mentioned above. I have all the page_id’s when they are created so I can reference them here as well to store the value in the builder option field.

    This question is a similar approach to this thread – One page website: http://support.themeblvd.com/forums/topic/one-page-website/

    What I like about the builder is that I can toggled the layout if it is a mobile or desktop display because you have the build in show/hide options.

    Is there a way to create a new layout using the Layout Builder (API)? Specifically, a new layout using existing framework elements.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #5803
    srumery
    Participant

    UPDATE: I used the themeblvd_add_sample_layout() function to create a sample layout which puts me really close to what I need. Is there an API call I can make to create a new layout using that sample layout?

    Then, I will need to use the theme options API to set the “Homepage Content” field to “Custom Layout” and use the new layout ID created using the themeblvd_add_sample_layout() function.

    If that all works out, it’s a huge time saver when I create a new instance of the child theme.

    #5805
    Jason Bobich
    Keymaster

    If I understand what you’re saying, there’s no API function that’s going to accomplish what you’re trying to do. A sample layout is something that the user has access to when creating a new layout. They can select a sample layout as a starting point for their new layout. However, I don’t think that’s what you’re wanting to do. You’re wanting a new layout to actually be created upon installing the theme.

    For this to be possible you need to understand what’s actually happening when you create a new custom layout. Custom layouts are actually posts of a custom post type called “tb_layout” behind the scenes. So, to create a new layout you need to create a new post of this type. You could possibly do this with wp_insert_post.

    $post_atts = array(
    	'post_title' => 'Layout Name',
    	'post_type' => 'tb_layout'
    );
    $new_layout_id = wp_insert_post( $post_atts );

    That’s the easy part. Then a custom layout post has meta data attached that actually makes up the elements and settings. These post meta entries are big arrays saved to the custom fields “elements” and “settings” —

    Note: When you call wp_insert_post, it returns the ID of the post created. That way, you can then use it to save your meta data (i.e. custom fields as some call them)

    $elements = array(
    	// ... All of your elements
    );
    add_post_meta( $new_layout_id, 'elements', $elements );
    
    $settings = array(
    	// ... Your layout's settings
    );
    add_post_meta( $new_layout_id, 'settings', $settings );

    Setting up those arrays to save to your meta data is going to be tricky. I’d suggest creating a custom layout with the actual Builder and then putting some temporary function on your site where you pull the meta data and print it out so you can see how the arrays are structured.

    [edit] And looking back at the Add Sample Layout tutorial, this part about coming up with the $elements is going to be similar to the advice given there where it starts with this text:

    One way to understand this would be to setup your sample layout the way you want manually in the Layout Builder, and then do what you always do when trying to rummage through someone else’s code – throw in some print_r()‘s!

    #5854
    srumery
    Participant

    This is another great reference piece. I decided to create a custom front page and skip the builder for the one page theme I am building. Now I have a great understanding of how the sample layouts work because I actually build one while creating my prototype. While I’m not actually using it, the process did help be decide on my development path and the outcome is better than I expected.

    Thanks again!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.