0

I have made my own plugin, which works fine, but I'm enqueueing (is that a word?) bootstrap, which then loads cross-site (admin). I found some workarounds, but the current workaround I'm trying throws an error (except on the plugin admin page itself).

Here's the code in myplugin/myplugin.php

class enq {     
function enqueue($hook) {
    if ((class_exists('myPlugin')) && ($_GET['page'] == 'myplugin')) {
        wp_enqueue_style('zsvstyle', plugins_url('/assets/app.min.css', __FILE__));
        wp_enqueue_style('perfect-scrollbar', plugins_url('assets/lib/perfect-scrollbar/css/perfect-scrollbar.min.css', __FILE__));
        wp_enqueue_style('iconic-font', plugins_url('assets/lib/material-design-icons/css/material-design-iconic-font.min.css', __FILE__));
        wp_enqueue_script('zsvscript', plugins_url('/assets/app.min.js', __FILE__));
        wp_enqueue_script('jquery', 'http://code.jquery.com/jquery-3.3.1.min.js', array(), null, true);
    }
}
}

function scripts() {
    add_action('admin_enqueue_scripts', array($this, 'enqueue'));
}

if (class_exists('myPlugin')) {

    $enq = new enq;
    $enq->scripts();

}

The error is

Notice: Undefined index: page in /var/www/html/mysite/wp-content/plugins/myplugin/myplugin.php on line 63

What is the correct way to only load the enqueue in the admin for this one plugin?

1

The WordPress documentation for admin_enqueue_scripts says that you can:

function load_custom_wp_admin_style($hook) {
        // Load only on ?page=mypluginname
        if($hook != 'toplevel_page_mypluginname') {
                return;
        }
        wp_enqueue_style( 'custom_wp_admin_css', plugins_url('admin-style.css', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );

Basically, you're being passed $hook and it should be the value of your page name, which is the name you've chosen.

  • Thank you. I tried this in my testing, but the problem was that I was using if($hook != 'mypluginname'), because I thought that toplevel_page_ was a description, but it has to be included. I now echoed $hook and of course it returned toplevel_page_myplugin (in my case). – Fid May 1 '18 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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