我正在尝试创建一个“联系表单”。在WP中使用的短代码。除了在WordPress网站上加载之外,一切正常。
当我将[contact_form]复制到页面或在页面上发布和预览时,它只打印文本。我做了正确的代码。
<?php
class Settings {
// Conact Form shortcode
public function allb_contact_form( $atts, $content = null ) {
//[contact_form]
//get the attribute_escape
$atts = shortcode_atts(
array(),
$atts,
'contact_form'
);
//return HTML
ob_start();
include '/lib/inc/thmeplates/contact-form.php';
return ob_get_clean();
add_shortcode( 'contact_form', 'allb_contact_form' );
}
} new Settings();
答案 0 :(得分:2)
您的add_shortcode()
函数调用需要引用包含的类。因此,如果从课外调用add_shortcode()
,则需要这样做。
class MyPlugin {
public static function baztag_func( $atts, $content = "" ) {
return "content = $content";
}
}
add_shortcode( 'baztag', array( 'MyPlugin', 'baztag_func' ) );
该示例来自https://codex.wordpress.org/Function_Reference/add_shortcode
如果从类中调用,则引用其自身内的类:
add_shortcode( 'baztag', array( $this , 'baztag_func' ) );
此外,您无法从输出短代码的同一功能中添加短代码。请尝试以下方法:
<?php
class Settings {
// Conact Form shortcode
public function __construct(){
add_shortcode( 'contact_form', array($this , 'allb_contact_form' ));
}
public function allb_contact_form( $atts, $content = null ) {
//[contact_form]
//get the attribute_escape
$atts = shortcode_atts(
array(),
$atts,
'contact_form'
);
//return HTML
ob_start();
include '/lib/inc/thmeplates/contact-form.php';
return ob_get_clean();
}
} new Settings();