vc_map:无法在textarea_html块

时间:2016-12-05 20:24:55

标签: php wordpress visual-c++ visual-composer

我使用vc_map()编写了一个自定义Visual Composer元素,该元素具有简单的标题,WISYWIG内容和背景图像。我的问题是我在内容块中使用的任何短代码都没有被检测到,并且是作为原始短代码吐出,或者,如果我通过do_shortcode调用,则在保存时被注释掉。

// Element Class
class vcInfoBox extends WPBakeryShortCode {

// Element Init
function __construct() {
  //  add_action( 'init', array( $this, 'vc_infobox_mapping' ) );
  $this->vc_infobox_mapping();
}

// Element Mapping
public function vc_infobox_mapping() {

    // Stop all if VC is not enabled
    if ( !defined( 'WPB_VC_VERSION' ) ) {
        return;
    }

    // Map the block with vc_map()
    vc_map(
        array(
            'name' => __('VC Infobox', 'text-domain'),
            'base' => 'vc_infobox',
            'description' => __('A simple callout box with backing image', 'text-domain'),
            'category' => __('Custom Elements', 'text-domain'),
            'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
            'params' => array(

                array(
                    'type' => 'textfield',
                    'holder' => 'h2',
                    'class' => 'title-class',
                    'heading' => __( 'Title', 'text-domain' ),
                    'param_name' => 'title',
                    // 'value' => __( 'Default value', 'text-domain' ),
                    'description' => __( 'Box Title', 'text-domain' ),
                    'admin_label' => false,
                    'weight' => 0,
                    'group' => 'Custom Group',
                ),

                array(
                    'type' => 'textarea_html',
                    'holder' => 'div',
                    // 'class' => 'text-class',
                    'heading' => __( 'Content', 'text-domain' ),
                    'param_name' => 'content',
                    'value' => __( '', 'text-domain' ),
                    'description' => __( 'Main content inside block', 'text-domain' ),
                    // 'admin_label' => false,
                    // 'weight' => 0,
                    'group' => 'Custom Group',
                ),

                array(
                    'type' => 'attach_image',
                    'holder' => 'img',
                    //'class' => 'text-class',
                    'heading' => __( 'Background Image', 'text-domain' ),
                    'param_name' => 'bgimg',
                    // 'value' => __( 'Default value', 'text-domain' ),
                    'description' => __( 'Image to be displayed behind callout block', 'text-domain' ),
                    'admin_label' => false,
                    'weight' => 0,
                    'group' => 'Custom Group',
                ),

                array(
                    'type' => 'dropdown',
                    'class' => '',
                    'heading' => __( 'Alignment', 'text-domain' ),
                    'param_name' => 'align',
                    'value' => array(
                      'Left' => "align-left",
                      'Right' => "align-right",
                    ),
                    'description' => __( 'Left or right alignment for callout block?', 'text-domain' ),
                    'admin_label' => false,
                    'weight' => 0,
                    'group' => 'Custom Group',
                ),

            ),
        )
    );

}


// Element HTML
public function vc_infobox_html( $atts, $content ) {

    // Params extraction
    extract(
        shortcode_atts(
            array(
                'title' => '',
                'bgimg' => 'bgimg',
                'align' => '',
            ),
            $atts
        )
    );

    $img_url = wp_get_attachment_image_src( $bgimg, "large");
    // die ( print_r($align, true) );

    // Fill $html var with data
    $html = '
    <div class="info-callout '. $align .'" style="background-image:url('. $img_url[0] .');">
        <div class="info-callout--content-wrap">
          <h2 class="info-callout--title">'. $title .'</h2>
          <div class="info-callout--content">'. $content .'</div>
        </div>
    </div>';

    return $html;

}

} // End Element Class

在functions.php中:

require_once( get_stylesheet_directory().'/vc-elements/custom-callout-block.php' );

add_action( 'vc_before_init', 'vc_before_init_actions' );

function vc_before_init_actions() {

    $InfoBox = new vcInfoBox();
    add_shortcode( 'vc_infobox', array( $InfoBox, 'vc_infobox_html' ) );

}

我将我的数组与VC中的默认“Text Block”元素进行了比较,它们似乎相匹配......有人知道我缺少什么吗?

1 个答案:

答案 0 :(得分:0)

这个问题似乎太老了。但这是解决方法。 更改此行

function __construct() {

add_action( 'init', array( $this, 'vc_infobox_mapping' ) );

function __construct() {
add_action( 'vc_before_init', array( $this, 'vc_infobox_mapping' ) );

还有一个在functions.php上的

add_action( 'vc_before_init', 'vc_before_init_actions' );

add_action( 'init', 'vc_before_init_actions' );