1

i'm writing a plugin which requires another plugin. So i have to check first if the other plugin exists in a correct version.

If yes i want to add a submenu page to the plugin page but here my code doesn't seem to work.

I think the problem is, that i only can check the existence of other plugins with a late hook (admin_init). But to add submenus i have to use an earlier hook (admin_menu). How to solve that?

class acf_multisite_sync {

    function __construct() {

        add_action('admin_init', array($this, 'acf_sync_init'));

    }

    function acf_sync_init() {

        $plugins = get_plugins();

        if (isset($plugins['advanced-custom-fields-pro/acf.php'])) {

            if ($plugins['advanced-custom-fields-pro/acf.php']['Version'] >= 5 
                && is_plugin_active('advanced-custom-fields-pro/acf.php')
                && is_main_site()) {

                add_action('admin_menu', array($this, 'acf_sync_submenu'));

            }

        }

    }

    function acf_sync_submenu() {

        add_submenu_page(
            'edit.php?post_type=acf-field-group', 
            'Multisite', 
            'Multisite', 
            'manage_options', 
            'acf-sync', 
            array($this, 'acf_sync_callback')
        );

    }

    function acf_sync_callback() {

        echo 'My Submenu Content';

    }

}

new acf_multisite_sync();

1 Answer 1

0

Ok i got it. The solution is to use an earlier hook for checking plugins. But then the function get_plugins() is not available so i have to include it for using. The working code:

class acf_multisite_sync {

    function __construct() {

        add_action('plugins_loaded', array($this, 'acf_sync_init'));

    }

    function acf_sync_init() {

        if (!function_exists('get_plugins')) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        $plugins = get_plugins();

        if (isset($plugins['advanced-custom-fields-pro/acf.php'])) {

            if ($plugins['advanced-custom-fields-pro/acf.php']['Version'] >= 5 
                && is_plugin_active('advanced-custom-fields-pro/acf.php')
                && is_main_site()) {

                add_action('admin_menu', array($this, 'acf_sync_submenu'), 100);

            }

        }

    }

    function acf_sync_submenu() {

        add_submenu_page(
            'edit.php?post_type=acf-field-group', 
            'Multisite', 
            'Multisite', 
            'manage_options', 
            'acf-sync', 
            array($this, 'acf_sync_callback')
        );

    }

    function acf_sync_callback() {

        echo 'My Submenu Content';

    }

}

new acf_multisite_sync();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.