我是使用过滤器的新手,但我很想制作我正在构建可扩展的插件。
在我的主插件中,我有以下类和函数:
class Skizzar_Admin_Theme_Pages {
// Return (array) the properties of all Skizzar Admin Theme admin pages
static function get_pages( $page_slug = '' ) {
$pages = array();
// Default page properties
$default_args = array(
'menu-title' => '',
'tab-title' => '',
'parent' => 'admin.php',
'in-menu' => false,
'has-tab' => true,
'tab-side' => false,
'top-level' => false,
);
$pages['sat-options-general'] = array_merge(
$default_args,
array(
'slug' => 'sat-options-general',
'menu-title' => _x( 'Admin Theme', 'Page title in the menu', 'skizzar_admin_theme' ),
'tab-title' => _x( 'Admin Theme Options', 'Option tab title', 'skizzar_admin_theme' ),
'title' => _x( 'Admin Theme Options', 'Option page title', 'skizzar_admin_theme' ),
'callback' => array( __CLASS__, 'display_general_options_page' ),
'in-menu' => true,
'top-level' => true,
)
);
$pages['sat-addons'] = array_merge(
$default_args,
array(
'slug' => 'sat-addons',
'menu-title' => _x( 'Addons', 'Page title in the menu', 'skizzar_admin_theme' ),
'tab-title' => _x( 'Addons', 'Option tab title', 'skizzar_admin_theme' ),
'title' => _x( 'Browse Addons', 'Option page title', 'skizzar_admin_theme' ),
'callback' => array( __CLASS__, 'display_addons_page' ),
'in-menu' => true,
)
);
return apply_filters( 'skizzar_admin_theme_tab', $pages, $default_args, $page_slug );
}
// Output the content of the requested options page
static function display_general_options_page() {
$page_info = self::get_pages( 'sat-options-general' );
include( plugin_dir_path( __FILE__ ) . 'inc/page-options-general.php' );
}
...
}
简而言之,此数组用于在该页面的顶部创建子菜单页面和选项卡等。你会看到我添加了过滤器:
apply_filters( 'skizzar_admin_theme_tab', $pages, $default_args, $page_slug );
对于函数get_pages()
,这是我可以从插件添加页面。
在我的插件中,我有以下功能:
function add_google_analytics_tab( $pages, $default_args, $page_slug ) {
$pages['sat-ga'] = array_merge(
$default_args,
array(
'slug' => 'sat-ga',
'menu-title' => _x( 'GA', 'Page title in the menu', 'skizzar_admin_theme' ),
'tab-title' => _x( 'GA', 'Option tab title', 'skizzar_admin_theme' ),
'title' => _x( 'GA', 'Option page title', 'skizzar_admin_theme' ),
'callback' => array( __CLASS__, 'display_ga_page' ),
'in-menu' => true,
)
);
// Return
if ( $page_slug ) {
if ( ! isset( $pages[ $page_slug ] ) ) {
return null;
}
return $pages[ $page_slug ];
}
return $pages;
}
function display_ga_page() {
echo 'GA Settings Go Here';
}
然后使用以下add_filter函数调用它:
add_filter( 'skizzar_admin_theme_tab', array(&$this, 'add_google_analytics_tab', 1, 3) );
希望这会将数组添加到我的主插件中的get_pages()
函数中,从而添加子菜单项和制表符等。
但是,我遇到了以下错误:
call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in ...plugin.php on line 235
Invalid argument supplied for foreach() in ...class-sat-menu.php on line 13
以下是class-sat-menu.php中引用的行
static function action_add_menu_entries() {
// Skizzar Admin Theme pages
foreach ( Skizzar_Admin_Theme_Pages::get_pages() as $page_info ) {
if ( ! $page_info['in-menu'] ) {
$page_info['parent'] = null;
}
if ($page_info['top-level']) {
add_menu_page(
$page_info['title'], // Page Title
( $page_info['menu-title'] ? $page_info['menu-title'] : $page_info['title'] ), // Menu Title
Skizzar_Admin_Theme_User::get_admin_cap(), // Capability
$page_info['slug'], // Slug
$page_info['callback'] // Callback
);
}
else {
add_submenu_page(
'sat-options-general',
$page_info['title'],
( $page_info['menu-title'] ? $page_info['menu-title'] : $page_info['title'] ),
Skizzar_Admin_Theme_User::get_admin_cap(),
$page_info['slug'],
$page_info['callback']
);
}
}
}
我无法理解错误是什么,所以任何建议或帮助都会受到赞赏