我工作与嵌套元素wp面包房。我正在尝试使用类和公共函数。
但无法让它工作。它没有注册。我想这可能是一个问题,因为我使用类。请找到什么错误。我不需要一个变通办法。我知道如何工作它与出成员函数。但我需要这样做使用成员函数
<?php
/*
* Element Description: Featured Block
*/
//featured block container
if ( class_exists( 'WPBakeryShortCodesContainer' ) ) {
class protechsaasFeaturedBlock extends WPBakeryShortCodesContainer {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'protechsaas_featured_block_mapping' ) );
add_shortcode( 'feature_container', array( $this, 'protechsaas_featured_block_html' ) );
}
// Element Mapping
public function protechsaas_featured_block_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
//Register "container" content element. It will hold all your inner (child) content elements
vc_map( array(
"name" => __("Feature Block", "protechsaas"),
"base" => "feature_container",
"as_parent" => array('only' => 'feature'), // Use only|except attributes to limit child shortcodes (separate multiple values with comma)
"content_element" => true,
"show_settings_on_create" => false,
"is_container" => true,
"params" => array(
// add params same as with any other content element
array(
"type" => "textfield",
"heading" => __("Block Title", "protechsaas"),
'holder' => 'h2',
'class' => 'sub-title-class',
"param_name" => "subtitle",
"description" => __("add the title for your features block", "protechsaas")
),
array(
"type" => "textfield",
"heading" => __("Title", "protechsaas"),
'holder' => 'h2',
'class' => 'title-class',
"param_name" => "title",
"description" => __("add the main title for your features block", "protechsaas")
),
array(
"type" => "textarea",
"heading" => __("Content", "protechsaas"),
'holder' => 'p',
'class' => 'content-class',
"param_name" => "content",
"description" => __("add the main content for your features block", "protechsaas")
),
array(
'type' => 'dropdown',
'holder' => 'div',
'class' => 'style-class',
'heading' => __( 'Block Style', 'protechsaas' ),
'param_name' => 'blockstyle',
'value' => array(
'1' => 'Feature white bg ',
'2' => 'Feature grey bg',
'3' => 'Feature icon bg none ',
),
'description' => __( 'choose you features block style', 'protechsaas' ),
'admin_label' => true,
'weight' => 0,
),
),
"js_view" => 'VcColumnView'
) );
}
// Element HTML
public function protechsaas_featured_block_html( $atts ,$features = null ) {
// Params extraction
extract(
shortcode_atts(
array(
'subtitle' => '',
'title' => '',
'content' => '',
'blockstyle' => '',
),
$atts
)
);
switch ($blockstyle) {
case '1':
$html = '
<section class="client-speak our-features padding-lg">
<div class="container">
<div class="row justify-content-center head-block">
<div class="col-md-10"> <span>'.$subtitle.'</span>
<h2>'.$title.'</h2>
<p class="hidden-xs">'.$content.'</p>
</div>
</div>
<ul class="row features-listing">
'.do_shortcode($features).'
</ul>
</div>
</section>';
break;
case '2':
$html = '
<section class="client-speak our-features padding-lg bg-white">
<div class="container">
<div class="row justify-content-center head-block">
<div class="col-md-10"> <span>'.$subtitle.'</span>
<h2>'.$title.'</h2>
<p class="hidden-xs">'.$content.'</p>
</div>
</div>
<ul class="row features-listing ico-bg">
'.do_shortcode($features).'
</ul>
</div>
</section>';
break;
case '3':
$html = '
<section class="client-speak our-features padding-lg">
<div class="container">
<div class="row justify-content-center head-block">
<div class="col-md-10"> <span>'.$subtitle.'</span>
<h2>'.$title.'</h2>
<p class="hidden-xs">'.$content.'</p>
</div>
</div>
<ul class="row features-listing bg-none">
'.do_shortcode($features).'
</ul>
</div>
</section> ';
break;
default:
$html = '
<section class="client-speak our-features padding-lg">
<div class="container">
<div class="row justify-content-center head-block">
<div class="col-md-10"> <span>'.$subtitle.'</span>
<h2>'.$title.'</h2>
<p class="hidden-xs">'.$content.'</p>
</div>
</div>
<ul class="row features-listing">
'.do_shortcode($features).'
</ul>
</div>
</section>';
break;
}
return $html;
}
}
}
//feature element
if ( class_exists( 'WPBakeryShortCode' ) ) {
class protechsaasFeature extends WPBakeryShortCode {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'protechsaas_feature_mapping' ) );
add_shortcode( 'protechsaas_feature', array( $this, 'protechsaas_feature_html' ) );
}
// Element Mapping
public function protechsaas_feature_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
vc_map( array(
"name" => __("Feature", "protechsaas"),
"base" => "feature",
"content_element" => true,
"as_child" => array('only' => 'feature_container'), // Use only|except attributes to limit parent (separate multiple values with comma)
"params" => array(
// add params same as with any other content element
array(
"type" => "textfield",
"heading" => __("title", "protechsaas"),
"param_name" => "title",
"description" => __("add the title for your feature.", "protechsaas")
),
array(
"type" => "textarea",
"heading" => __("Content", "protechsaas"),
'holder' => 'p',
'class' => 'content-class',
"param_name" => "content",
"description" => __("add the content for your feature", "protechsaas")
),
array(
'type' => 'attach_image',
'holder'=> 'div',
'class' => '',
'heading' => __('Icon image', 'protechsaas'),
'param_name' => 'iconimage',
'description' => __('Choose an image for icon if ypu want use your own icons else keep it blank and choose icon from next field', 'protechsaas'),
),
array(
'type' => 'dropdown',
'holder' => 'div',
'class' => 'style-class',
'heading' => __( 'Saas Icons', 'protechsaas' ),
'param_name' => 'icon',
'value' => array(
'icon-analytics' => 'analytics ',
'icon-responsive' => 'responsive',
'icon-support' => 'support',
'icon-settings' => 'settings',
'icon-file' => 'file',
'icon-graphic' => 'graphic',
),
'description' => __( 'choose you features block style', 'protechsaas' ),
'admin_label' => true,
'weight' => 0,
),
)
) );
}
// Element HTML
public function protechsaas_feature_html( $atts ) {
// Params extraction
extract(
shortcode_atts(
array(
'iconimage' => '',
'title' => '',
'content' => '',
'icon' => '',
),
$atts
)
);
if($iconimage != null){
$bg=wp_get_attachment_image_src($iconimage,'full');
$iconcontent = '<img src="'.$bg[0].'" alt="icon" class="img-fluid"/>';
}
else {
$iconcontent ='<span class="'.$icon.'"></span>';
}
$html='
<li class="col-md-4">
<div class="inner"> <span class="icon">'.$iconcontent.'</span>
<h3>'.$title.'</h3>
<p>'.$content.'</p>
</div>
</li>
';
return $html;
}
}
}
// Element Class Init
new protechsaasFeaturedBlock();
new protechsaasFeature();
?>
4条答案
按热度按时间nmpmafwu1#
我也遇到过同样的问题,花了一些时间来解决这个问题。我们不想脱离我们的命名空间环境,并保持我们的类名一致。
有一个额外的未记录的属性可以传递给vc_map函数,那就是
php_class_name
。所以你的代码应该是:
这也适用于名称空间:
ccrfmcuu2#
据我所知,这没有文档,但是如果你要扩展
WPBakeryShortCodesContainer
类,你的类名必须以WPBakeryShortCode_
为前缀,并且后缀必须与你使用的短代码库的名称相匹配。因此,如果在
vc_map()
中使用:然后将类声明更改为:
我知道这是没有意义的,但是经过大量的测试,这就是我的代码失败的原因,我认为这是因为我也将它声明为一个类,而不是直接调用
vc_map()
。您甚至不需要声明一个短代码就可以工作,所以我猜
WPBakeryShortCodesContainer
类中的某个东西实际上注册了来自base
参数的短代码,如果那里还没有的话?是的,如果你想保持你的名字空间干净/一致的话,这个mega是很糟糕的。
wfsdck303#
我复制了这个例子作为嵌套元素。我发现你的代码中有一个打字错误。这一行:
功能名称为
feature
,而不是protechsaas_feature
。定义如下:因此,您的代码应该变为:
qgelzfjb4#
我尝试了这里的所有答案,但没有成功(使用WPBakery 6.9)。
vc_map参数“php_class_name”什么也不做...不知道为什么有些人让它工作。关于自定义类名的无文档规则是领先了一步,它确实需要那个“WPBakeryShortCode_"+“base”,但还不够。
所以我设法修复了一些问题。同样覆盖了核心类的一个方法,我的答案如下:https://stackoverflow.com/a/75721948/9039682