Home>Support>Creating pagebuilder layout upon post creation

Creating pagebuilder layout upon post creation

Notice: This thread is over two years old; the information may be outdated. Please consider creating a new thread if you require free support. If you have an active SiteOrigin Premium license, you can email our premium support desk at [email protected].

Hey,

I’m stuck on a problem. I want to generate Page Builder layout automatically upon creating a new post in a specific post type of mine. My hopes are that this can be accomplished by using data saved in a previous post of the same type that has the layout save in its meta field panels_data in the database. The function saves the data to the new posts meta fine. But when entering the post for editing, there is no Page Builder layout content to be found and the editor is still in text mode.

The purpose is to not have to repeat the same process of creating layout and editing the widgets over and over again if making a bunch of new posts.

Any ideas? Function in my functions.php follows:

function save_panel_meta( $post_id, $post, $update ) {
	//If the post is newly created
	if ($post->post_status == "auto-draft") {
		//Different panels to be generated for different post types fresh posts
		switch ($post->post_type) {

			case 'kronika':
				add_post_meta($post_id, 'panels_data', 'a:3:{s:7:"widgets";a:4:{i:0;a:5:{s:14:"ad_widget_type";s:8:"Internal";s:16:"ad_widget_layout";s:10:"Horizontal";s:15:"ad_widget_color";s:5:"Green";s:12:"_sow_form_id";s:13:"56b393aae2296";s:11:"panels_info";a:4:{s:5:"class";s:16:"Ad_Slider_Widget";s:4:"grid";i:0;s:4:"cell";i:0;s:2:"id";i:0;}}i:1;a:5:{s:14:"ad_widget_type";s:8:"Internal";s:16:"ad_widget_layout";s:10:"Horizontal";s:15:"ad_widget_color";s:5:"Green";s:12:"_sow_form_id";s:13:"56b393b34d3fd";s:11:"panels_info";a:4:{s:5:"class";s:16:"Ad_Slider_Widget";s:4:"grid";i:0;s:4:"cell";i:0;s:2:"id";i:1;}}i:2;a:5:{s:14:"ad_widget_type";s:8:"Internal";s:16:"ad_widget_layout";s:10:"Horizontal";s:15:"ad_widget_color";s:5:"Green";s:12:"_sow_form_id";s:13:"56b393afe1760";s:11:"panels_info";a:4:{s:5:"class";s:16:"Ad_Slider_Widget";s:4:"grid";i:0;s:4:"cell";i:1;s:2:"id";i:2;}}i:3;a:4:{s:14:"ad_widget_type";s:8:"Internal";s:16:"ad_widget_layout";s:10:"Horizontal";s:15:"ad_widget_color";s:5:"Green";s:11:"panels_info";a:4:{s:5:"class";s:16:"Ad_Slider_Widget";s:4:"grid";i:0;s:4:"cell";i:1;s:2:"id";i:3;}}}s:5:"grids";a:1:{i:0;a:2:{s:5:"cells";i:2;s:5:"style";a:0:{}}}s:10:"grid_cells";a:2:{i:0;a:2:{s:4:"grid";i:0;s:6:"weight";d:0.5;}i:1;a:2:{s:4:"grid";i:0;s:6:"weight";d:0.5;}}}');

				break;
			
			default:
				# code...
				break;
		}
	}
	
    
}
add_action( 'save_post', 'save_panel_meta', 10, 3 );

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

  1. 9 years, 5 months ago Greg Priday
    Hi, I Work Here

    Based on a quick skim of your code, the main issue is that you’re setting the value of panels_data to a string. There is actually a chance that WordPress post meta can handle this, but you might have better results if you pass that through unserialize first.

    add_post_meta($post_id, 'panels_data', unserialize( 'a:3:{s:7:"widgets";a:4:{i:0;a:5:{s:14:"ad_widget_type";s:8:"Internal";s:16:"ad_widget_layout";s:10:"Horizontal";s:15:"ad_widget_color";s:5:"Green";s:12:"_sow_form_id";s:13:"56b393aae2296";s:11:"panels_info";a:4:{s:5:"class";s:16:"Ad_Slider_Widget";s:4:"grid";i:0;s:4:"cell";i:0;s:2:"id";i:0;}}i:1;a:5:{s:14:"ad_widget_type";s:8:"Internal";s:16:"ad_widget_layout";s:10:"Horizontal";s:15:"ad_widget_color";s:5:"Green";s:12:"_sow_form_id";s:13:"56b393b34d3fd";s:11:"panels_info";a:4:{s:5:"class";s:16:"Ad_Slider_Widget";s:4:"grid";i:0;s:4:"cell";i:0;s:2:"id";i:1;}}i:2;a:5:{s:14:"ad_widget_type";s:8:"Internal";s:16:"ad_widget_layout";s:10:"Horizontal";s:15:"ad_widget_color";s:5:"Green";s:12:"_sow_form_id";s:13:"56b393afe1760";s:11:"panels_info";a:4:{s:5:"class";s:16:"Ad_Slider_Widget";s:4:"grid";i:0;s:4:"cell";i:1;s:2:"id";i:2;}}i:3;a:4:{s:14:"ad_widget_type";s:8:"Internal";s:16:"ad_widget_layout";s:10:"Horizontal";s:15:"ad_widget_color";s:5:"Green";s:11:"panels_info";a:4:{s:5:"class";s:16:"Ad_Slider_Widget";s:4:"grid";i:0;s:4:"cell";i:1;s:2:"id";i:3;}}}s:5:"grids";a:1:{i:0;a:2:{s:5:"cells";i:2;s:5:"style";a:0:{}}}s:10:"grid_cells";a:2:{i:0;a:2:{s:4:"grid";i:0;s:6:"weight";d:0.5;}i:1;a:2:{s:4:"grid";i:0;s:6:"weight";d:0.5;}}}') );
    
  2. 9 years, 5 months ago Greg Priday
    Hi, I Work Here

    On a side note, I created this custom post type builder for Page Builder a little while ago. I haven’t got around to releasing it (or more specifically writing up documentation, training support, etc.), but maybe you can get some ideas on how to use Page Builder with custom post types there.

    https://github.com/siteorigin/so-cpt-builder

  3. 9 years, 5 months ago thajjd

    Thank you for answering so quickly. The data saves to the custom field “panels_data” just fine. However, I suspect that there is some kind of function that as to be triggered manually that uses that data. Interesting plugin, skimmed the source code but I really am way to tired to make any sense of it enough to come up with a solution to my problem with the help of it.

    If you know that you have written a function that creates a default layout for a new post upon post creation I would be most thankful if you chose to share it with me.

  4. 9 years, 5 months ago thajjd

    Is there perhaps some way I could use the import layout function from the pagebuilder plugin? If so, how would I go about to use this?

    /**
     * Ajax handler to import a layout
     */
    function siteorigin_panels_ajax_import_layout(){
    	if( empty( $_REQUEST['_panelsnonce'] ) || !wp_verify_nonce($_REQUEST['_panelsnonce'], 'panels_action') ) wp_die();
    
    	if( !empty($_FILES['panels_import_data']['tmp_name']) ) {
    		header('content-type:application/json');
    		$json = file_get_contents( $_FILES['panels_import_data']['tmp_name'] );
    		@unlink( $_FILES['panels_import_data']['tmp_name'] );
    		echo $json;
    	}
    	wp_die();
    }
    add_action('wp_ajax_so_panels_import_layout', 'siteorigin_panels_ajax_import_layout');
  5. 9 years, 5 months ago thajjd

    Shameless Bump…

  6. 9 years, 5 months ago Greg Priday
    Hi, I Work Here

    Hi thajjd

    Unfortunately we don’t quite have the capacity to help to help with custom development at the moment. We’re currently at full capacity with standard customer support and development on Page Builder and our other plugins.

    I’m hoping that we’ll be able to launch a developer level support program that’ll allow you to ask a SiteOrigin trained developer in depth questions. Assuming there’s enough demand for an offering like this.

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