Skip to content

Instantly share code, notes, and snippets.

@jonwaldstein
Created December 9, 2016 22:37
Show Gist options
  • Save jonwaldstein/04f1b1dde4b93988bf0253e26b1dac8e to your computer and use it in GitHub Desktop.
Save jonwaldstein/04f1b1dde4b93988bf0253e26b1dac8e to your computer and use it in GitHub Desktop.
Carbon Fields Page Builder Add-on
<?php
use Carbon_Fields\Container;
use Carbon_Fields\Field;
use Carbon_Fields\Field\Complex_Field;
Container::make( 'post_meta', 'Page Builder' )//PAGE BUILDER FIELDS
->show_on_post_type('page')
->add_fields( array(
Field::make( 'complex', 'crb_layouts' )
->set_layout('tabbed-vertical')
->setup_labels(array(
'plural_name' => 'Sections',
'singular_name' => 'Section',
))
->add_fields( 'dynamic_section', array(
Field::make("separator", "section_settings"),
Field::make('checkbox', 'full_width_section')->set_width(20),
Field::make('checkbox', 'content_contained')->set_width(20),
Field::make('text', 'section_class')->set_width(30),
Field::make('image', 'section_background_image')->set_value_type( 'url' )->set_width(30),
Field::make('text', 'section_heading'),
Field::make( 'complex', 'columns' )
->set_layout('tabbed-vertical')
->set_max(6)
->setup_labels( array(
'plural_name' => 'Columns',
'singular_name' => 'Column'
))
->add_fields( 'Column', array(
Field::make('text', 'column_class'),
Field::make( 'rich_text', 'column_content' )->set_width(50)
))
)),
));
function carbon_parse_shortcodes($meta_string){//for use of shortcodes in rich text
return apply_filters('the_content', $meta_string);
}
function bg_image($bg){ //background image function
if ($bg){
$background_image = "background:url('".$bg."');background-size:cover;background-repeat:no-repeat;background-position: center center;";
return $background_image;
}else{
return '';
}
}
function carbon_display_page_builder() {//call function in pages that will use this
$layouts = carbon_get_the_post_meta( 'crb_layouts', 'complex' );
if (!empty($layouts)){
foreach ( $layouts as $layout ) {
$function_name = 'carbon_print_layout';
if ( function_exists( $function_name ) ) {
call_user_func( $function_name, $layout );
}
}
}
}
function carbon_print_layout($layout) {//view fields
?>
<div class="section <?= $layout['full_width_section'] ? 'container-fluid' : 'container'; ?> <?= $layout['content_contained'] ? 'content-contained' : ''; ?> <?= $layout['section_class']; ?>" style="<?=bg_image($layout['section_background_image'])?>">
<?php if ( $layout['_type'] === '_dynamic_section' ): ?>
<?php if ( $layout['section_heading'] ): ?>
<div class="row row-heading">
<div class="col-sm-12 text-center">
<h3><?= $layout['section_heading']; ?></h3>
</div>
</div>
<?php endif; ?>
<div class="row flexible-content">
<?php $columns = $layout['columns']; ?>
<?php if (!empty($columns)): ?>
<?php foreach($columns as $column ): ?>
<?php $max_columns = count($columns);
$col_html = '';
if($max_columns === 5) {
$col_class = 15;
} else {
$col_class = (12/$max_columns);
}
?>
<div class="<?= $max_columns === 5 ? 'col-sm-2 ' : ''; ?> col-sm-<?=$col_class;?> <?= $column['column_class']; ?>">
<?= carbon_parse_shortcodes($column['column_content']); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment