Search code examples
phpwordpressfilterextensibility

Php wordpress filter not working


In my main plugin, I have a function called get_pages(). This function contains an array for each admin page for the plugin, here is the function:

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,
        )
    );

    if ( $page_slug ) {
        if ( ! isset( $pages[ $page_slug ] ) ) {
            return null;
        }
        return $pages[ $page_slug ];
    }

    return apply_filters( 'skizzar_admin_theme_tab', $pages, $default_args, $page_slug ); 

}

This function gives information for a page called sat-options-general and sat-addons.

I have added a filter at the bottom of the function so that I can extend this plugin by creating an addon plugin.

In my addon plugin I have the following functions:

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-ga'] = array_merge(
            $default_args,
            array(
                'slug' => 'sat-ga',
                'menu-title' => 'GA',
                'tab-title' => 'GA',
                'title' => 'GA',
                'callback' => 'display_ga_page',
                'in-menu' => true,
            )
        );
        return $pages;
}
function display_ga_page() {
    //$page_info = self::get_pages( 'sat-addons' );
    //include( plugin_dir_path( __FILE__ ) . 'inc/page-addons.php' );
    echo 'GA Settings Go Here';
}

The first being the $pages set up that hooks into the custom filter I made as follows:

function init() {

...

    add_filter( 'skizzar_admin_theme_tab', array(&$this, 'add_google_analytics_tab', 1, 3) );

...

}

The second function is the call back, which should just echo out "GA Settings go here" on the newly created page.

However, the result is that my plugin menu completely dissapears and I get the following errors in the debug.log file:

[18-Mar-2016 15:59:52 UTC] PHP Warning:  call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in /Applications/MAMP/htdocs/skizzar-local/wp-includes/plugin.php on line 235
[18-Mar-2016 15:59:52 UTC] PHP Warning:  Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/skizzar-local/wp-content/plugins/skizzar-admin-theme-master/class-sat-menu.php on line 13

I can't figure out at all what is going on here and really want to find a solution - I'm sure what I'm trying to do isn't tat hard, but a lot of the main plugin draws on info from the $pages array, so it's important that I am able to inject this code into my plugin.


Solution

  • I think you have a typo in add_filter call, you didn't close a bracket after add_google_analytics_tab:

    function init() {
    
    ...
    
        add_filter( 'skizzar_admin_theme_tab', array(&$this, 'add_google_analytics_tab'), 1, 3 );
    
    ...
    
    }