我想看看他是否正确地使用此代码来开发联系表单的自定义短代码,以便为我的网站创建。
我看过wp codex想要使用类,所以我想出了这个代码,如果这样可行。
<?php
class MyPlugin {
// Conact Form shortcode
public static function allb_contact_form( $atts, $content = " " ) {
//[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', array( $this , 'allb_contact_form' ) );
但它并没有给我提供错误信息。一个人告诉我把 add_shortcode( 'contact_form', array( $this , 'allb_contact_form' ) );
放在代码外面。
答案 0 :(得分:1)
如果您打算使用课程,无论您在哪里声明插件添加
$myplugin = new MyPlugin();
add_shortcode( 'contact_form', [ $myplugin , 'allb_contact_form' ] );
从类中删除add_shortcode。它甚至不是有效的代码。