Reputation: 3697
I am new to using wordpress filters. I am writing a plugin which I want to be extensible. I have the following function in my plugin that creates tabs on the plugin page:
static function get_pages( $page_slug = '' ) {
$pages = array();
// Add tabs to network admin page if global settings enabled
$is_network_only = ( is_multisite()) ? true : false;
// Default page properties
$default_args = array(
'menu-title' => '',
'tab-title' => '',
'parent' => 'themes.php',
'in-menu' => false,
'has-tab' => true,
'has-network-tab' => false,
'tab-side' => false,
'network' => 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,
'has-network-tab' => $is_network_only,
'network' => $is_network_only
)
);
// Return
if ( $page_slug ) {
if ( ! isset( $pages[ $page_slug ] ) ) {
return null;
}
return $pages[ $page_slug ];
}
return $pages;
}
To make it extensible, I change the return statement to the following:
// Return
if ( $page_slug ) {
if ( ! isset( $pages[ $page_slug ] ) ) {
return null;
}
return $pages[ $page_slug ];
}
return apply_filters( 'skizzar_admin_theme_tab', $pages );
Then, I have created another plugin to test out this filter and add another tab, so I add the following code:
function add_google_analytics_tab( $pages ) {
$pages['sat-google-analytics'] = array_merge(
$default_args,
array(
'slug' => 'sat-google-analytics',
'menu-title' => _x( 'Google Analytics', 'Page title in the menu', 'skizzar_admin_theme' ),
'tab-title' => _x( 'Google Analytics', 'Option tab title', 'skizzar_admin_theme' ),
'title' => _x( 'Google Analytics', 'Option page title', 'skizzar_admin_theme' ),
'callback' => array( __CLASS__, 'display_general_options_page' ),
'in-menu' => true,
'has-network-tab' => true,
'network' => true
)
);
}
add_filter( 'skizzar_admin_theme_tab', 'add_google_analytics_tab' );
But nothing happens - in fact, my whole plugin page has gone missing. Like I say, I'm new to using filters, can anyone help me understand where I've gone wrong
Upvotes: 0
Views: 87
Reputation: 480
Here for your correct code
// Return
if ( $page_slug ) {
if ( ! isset( $pages[ $page_slug ] ) ) {
return null;
}
return $pages[ $page_slug ];
}
return apply_filters( 'skizzar_admin_theme_tab', $pages, $default_args ); //values
Then:
function add_google_analytics_tab( $pages, $default_args ) {
$pages['sat-google-analytics'] = array_merge(
$default_args,
array(
'slug' => 'sat-google-analytics',
'menu-title' => _x( 'Google Analytics', 'Page title in the menu', 'skizzar_admin_theme' ),
'tab-title' => _x( 'Google Analytics', 'Option tab title', 'skizzar_admin_theme' ),
'title' => _x( 'Google Analytics', 'Option page title', 'skizzar_admin_theme' ),
'callback' => array( __CLASS__, 'display_general_options_page' ),
'in-menu' => true,
'has-network-tab' => true,
'network' => true
)
);
return $pages;
}
add_filter( 'skizzar_admin_theme_tab', 'add_google_analytics_tab', 1, 2 ); //1 priority, 2 accepted_args
If $page_slug
include in hook.
static function get_pages( $page_slug = '' ) {
$pages = array();
// Add tabs to network admin page if global settings enabled
$is_network_only = ( is_multisite()) ? true : false;
// Default page properties
$default_args = array(
'menu-title' => '',
'tab-title' => '',
'parent' => 'themes.php',
'in-menu' => false,
'has-tab' => true,
'has-network-tab' => false,
'tab-side' => false,
'network' => 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,
'has-network-tab' => $is_network_only,
'network' => $is_network_only
)
);
return apply_filters( 'skizzar_admin_theme_tab', $pages, $default_args, $page_slug );
}
Then:
function add_google_analytics_tab( $pages, $default_args, $page_slug ) {
// Return
if ( $page_slug ) {
if ( ! isset( $pages[ $page_slug ] ) ) {
return null;
}
return $pages[ $page_slug ];
}
$pages['sat-google-analytics'] = array_merge(
$default_args,
array(
'slug' => 'sat-google-analytics',
'menu-title' => _x( 'Google Analytics', 'Page title in the menu', 'skizzar_admin_theme' ),
'tab-title' => _x( 'Google Analytics', 'Option tab title', 'skizzar_admin_theme' ),
'title' => _x( 'Google Analytics', 'Option page title', 'skizzar_admin_theme' ),
'callback' => array( __CLASS__, 'display_general_options_page' ),
'in-menu' => true,
'has-network-tab' => true,
'network' => true
)
);
return $pages;
}
add_filter( 'skizzar_admin_theme_tab', 'add_google_analytics_tab', 1, 3 ); //1 priority, 3 accepted_args
Upvotes: 1