Skip to content

Instantly share code, notes, and snippets.

@Webcreations907
Last active June 8, 2022 13:19
Show Gist options
  • Save Webcreations907/ff0b068c5c364f63e45d to your computer and use it in GitHub Desktop.
Save Webcreations907/ff0b068c5c364f63e45d to your computer and use it in GitHub Desktop.
Example VC Nesting
<?php
/************************************************************************
* Example Nested For VC
* vc_map() stuff should only be included when VC is enabled.
*
* This is just for a copy/paste test purpose.
*************************************************************************/
// Parent container
vc_map( array(
'name' => __( 'WBC Item' , 'textdomain' ),
'base' => 'wbc_item',
'icon' => 'icon-wpb-row',
'description' => __( 'Container for Item', 'textdomain' ),
'as_parent' => array('only' => 'wbc_inner_item'), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
'content_element' => true,
'show_settings_on_create' => true,
'params' => array(
//BEGIN ADDING PARAMS
array(
'type' => 'textfield',
'heading' => __( 'Test Heading', 'textdomain' ),
'param_name' => 'heading',
'description' => __( 'Heading will be displayed at the top of items', 'textdomain' ),
),
//END ADDING PARAMS
),
"js_view" => 'VcColumnView'
) );
// Nested Element
vc_map( array(
'name' => __('WBC Items', 'textdomain'),
'base' => 'wbc_inner_item',
'description' => __( 'Items "Item".', 'textdomain' ),
'icon' => 'icon-wpb-row',
'content_element' => true,
'as_child' => array('only' => 'wbc_item'), // Use only|except attributes to limit parent (separate multiple values with comma)
'params' => array(
//BEGIN ADDING PARAMS
array(
'type' => 'textarea_html',
'holder' => 'div',
'heading' => __( 'Content', 'textdomain' ),
'param_name' => 'content',
'value' => __( '<p>Some default text here.</p>', 'textdomain' )
),
//END ADDING PARAMS
),
) );
// A must for container functionality, replace Wbc_Item with your base name from mapping for parent container
if(class_exists('WPBakeryShortCodesContainer'))
{
class WPBakeryShortCode_Wbc_Item extends WPBakeryShortCodesContainer {
}
}
// Replace Wbc_Inner_Item with your base name from mapping for nested element
if(class_exists('WPBakeryShortCode'))
{
class WPBakeryShortCode_Wbc_Inner_Item extends WPBakeryShortCode {
}
}
// Shortcodes, just for this example for testing :)
// I keep the shortcodes in their own file so that
// they don't rely on VC for outputting them, so
// shortcodes will work when VC not enabled/installed
// as some users may not want to use VC.
if(!function_exists('wbc_item_output')){
function wbc_item_output( $atts, $content = null){
$atts = extract(shortcode_atts(
array(
'heading' => '',
),$atts )) ;
$html = '';
$html .= '<div class="item-container">';
if(!empty($heading)){
$html .= '<h4>'.$heading.'</h4>';
}
$html .= do_shortcode( $content );
$html .= '</div>';
return $html;
}
add_shortcode( 'wbc_item' , 'wbc_item_output' );
}
if(!function_exists('wbc_inner_item_output')){
function wbc_inner_item_output($atts, $content = null){
// $atts = extract(shortcode_atts(
// array(
// 'testing' => '',
// ),$atts )) ;
$html = '';
$html .= '<div class="inner-item">';
$html .= do_shortcode( $content );
$html .= '</div>';
return $html;
}
add_shortcode( 'wbc_inner_item' , 'wbc_inner_item_output' );
}
/************************************************************************
* END Example Nested
*************************************************************************/
?>
@sigaev-pro
Copy link

Thank you for good example!

Do you know if your can access container's attrs from child (wbc_inner_item) ?

@isuke01
Copy link

isuke01 commented Dec 10, 2015

Try:
// For Nested element vcfile

if ( class_exists( 'WPBakeryShortCode' ) ) {
    class WPBakeryShortCode_Vcfile extends WPBakeryShortCode {
        protected function content( $atts, $content = null){
            var_dump($atts); // debug
            extract(shortcode_atts(array(
                'file_label' => '',
                'vclink_file' => '',
                'vclink_file_icon' => '',
            ), $atts));
            $GLOBALS['file_count'] = empty($GLOBALS['file_count']) ? 0 : $GLOBALS['file_count'];
            $x = $GLOBALS['file_count'];
            $GLOBALS['vcfileslist'][$x] = array(
                'file_label' => $file_label,
                'vclink_file' => $vclink_file,
                'vclink_file_icon' => $vclink_file_icon,
                );
          // we want count fields
            $GLOBALS['file_count']++;
        }
    }
}

// map shortcode vcfiles <- base class (parent)

add_shortcode( 'vcfiles', 'vc_files_upload_shortcode' );
function vc_files_upload_shortcode( $atts, $content=null ) {
  /* extract( shortcode_atts( array(
      'file_label' => 'nice heading'
   ), $atts ) );*/

$vcfiles = array();
//parse shortcode on on all vcfiles to get the array of  data. See WPBakeryShortCode_Marker class
do_shortcode($content);
//parsing slide shortcodes returns GLOBAL slide data array
if(!empty($GLOBALS['vcfileslist'])) {
    //echo "<pre>"; print_r($GLOBALS['vcfiles']); echo "</pre>";
    $vcfiles = $GLOBALS['vcfileslist'];

    //reset globals, commented now, we wanna see var dump
    //unset($GLOBALS['vcfileslist']);
    //unset($GLOBALS['file_count']);
}
var_dump($GLOBALS['file_count']);
var_dump($GLOBALS['vcfileslist']);
   return "<div>{$vcfiles} </div>";
}

@saroarhossain57
Copy link

Thanks a lot

@saitaosla
Copy link

Been looking this for some time now, thank you very much!!!!

@code4guate
Copy link

example.php is exactly what I'm looking to do. However I am having a hard time returning to the parent to edit it's attributes. Was that what isuke01 commented Dec 10 supposed to fix? Just pasting it in didn't help. (sorry I'm new to VC).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment