在使用PHP oop概念开发Contact Form 7扩展时遇到一个奇怪的问题。
wpcf7_admin_after_additional_settings
动作在我将函数放入类中时不起作用,如果我将该动作的函数置于类之外,则其工作正常。
我的代码正在关注。
<?php
// This function is working if you put outside the class
function action_wpcf7_admin_after_additional_settings( $cf7pp_admin_after_additional_settings )
{
echo "Outside the class";
}
class Cf7pe_Admin
{
function __construct()
{
add_filter( 'wpcf7_editor_panels', array($this,'cf7pe_admin_editor_pannels') );
add_action( 'wpcf7_admin_after_additional_settings', array($this,'action_wpcf7_admin_after_additional_settings'), 10, 1 );
}
// This funciton is not working within class
function action_wpcf7_admin_after_additional_settings( $cf7pp_admin_after_additional_settings )
{
echo "Within class";
}
function cf7pe_admin_editor_pannels ( $panels )
{
$new_page = array(
'Stripe' => array(
'title' => __( 'Paypal', 'contact-form-7' ),
'callback' => 'action_wpcf7_admin_after_additional_settings'
)
);
$panels = array_merge($panels, $new_page);
return $panels;
}
}
$cf7pe_admin = new Cf7pe_Admin();
如果内部类函数使其起作用,请给出您的建议。请为此提供您宝贵的意见。
答案 0 :(得分:1)
尝试此代码。您错过了array($this,
中的'callback' =>
class Cf7pe_Admin
{
function __construct()
{
add_filter( 'wpcf7_editor_panels', array($this,'cf7pe_admin_editor_pannels') );
add_action( 'wpcf7_admin_after_additional_settings', array($this,'action_wpcf7_admin_after_additional_settings'), 10, 1 );
}
// This funciton is not working within class
function action_wpcf7_admin_after_additional_settings( $cf7pp_admin_after_additional_settings )
{
echo "Within class";
}
function cf7pe_admin_editor_pannels ( $panels )
{
$new_page = array(
'Stripe' => array(
'title' => __( 'Paypal', 'contact-form-7' ),
'callback' => array($this,'action_wpcf7_admin_after_additional_settings')
)
);
$panels = array_merge($panels, $new_page);
return $panels;
}
}
$cf7pe_admin = new Cf7pe_Admin();