使我的wordpress插件可扩展

I have written a wordpress plugin. The plugin adds a plugin page, and along the top of that page is tabs to other pages (the tabs add a query string to the url, depending on the query string depends on what options are shown).

I am now trying to make the plugin extensible by adding action hooks and filters - I'm very new to using these.

Here is the first part of my Class from my main plugin file:

class Skizzar_Admin_Theme_Pages {

// Return (array) the properties of all Skizzar Admin Theme admin pages
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 ); 

}

// 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' );
}

I have added a filter to the first function which allows me to successfully extend it. Now I'm struggling to figure out how I externally add a function similar to the function display_general_options_page.

To test the exensible nature of my plugin, I have created a second plugin. My current code is this:

function add_google_analytics_tab( $pages, $default_args, $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( $this, 'display_google_analytics_page' ),
        'in-menu' => false,
        'has-network-tab' => true,
        'network' => true
    )
);
// Return
if ( $page_slug ) {
    if ( ! isset( $pages[ $page_slug ] ) ) {
        return null;
    }
    return $pages[ $page_slug ];
}
return $pages;
}
add_filter( 'skizzar_admin_theme_tab', 'add_google_analytics_tab', 1, 3 );

This uses the filter in my main plugin to add another tab to the plugins page. Now I need to add the function which includes the page content when the tab is clicked.

I have added the following to my addon plugin:

function display_google_analytics_page() {
    $page_info = get_pages( 'sat-google-analytics' );
    include( plugin_dir_path( __FILE__ ) . 'inc/hello.php' );
}

Which should display the contents of a test file called hello.php (which just echos "hello". But nothing shows.

What I can't understand is how I just add this code in my addon plugin to the code in my main plugin

You need to understand the load pattern for this..basically

  1. plugins

  2. themes

  3. etc

Unfortunately, there is not really a clear way to specify load a certain plugin first etc. So you have to account for that when designing your plugin.

For example in your plugin:

   add_action('init', 'my_filter_func');

   function my_filter_func(){
       $filt= 'unchanged';
       echo apply_filters('filter_me', $filt);
       exit;
   }

Now this function will run on the init hook which is after all plugins and themes have loaded. So you can filter from plugins or themes but before content is displayed.