2017-01-03 68 views
0

我想创建一个'联系表'短代码在WP中使用。除了在WordPress网站上加载外,所有的工作都很好。WordPress的短代码发行v2

enter image description here

当我复制[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(); 

回答

1

您的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(); 
+0

我想要wp codex read,但仍不能打印出表格。 –

+0

当我从类中添加add_shortcode时会出现错误消息。注意:do_shortcode_tag被错误地调用。尝试解析没有有效回调的简码:contact_form请参阅WordPress中的调试以获取更多信息。 (这条消息是在4.3.0版本中添加的。)在/Users/brandonpowell/sites/valet/wordpress-development/web/wp/wp-includes/functions.php on line 4091 –

+0

@BrandonPowell我增加了更多细节,应该解决你的问题 –