Load plugin scripts and styles only on plugin page

When you register a plugin option page you get a hook from the registration function:

$hook = add_menu_page(
    'T5 Demo',        // page title
    'T5 Demo',        // menu title
    'manage_options', // capability
    't5-demo',        // menu slug
    'my_render_page'  // callback function
);

Use this hook to enqueue the scripts and styles:

add_action( "admin_print_styles-$hook", "my_enqueue_style" );
add_action( "admin_print_scripts-$hook", "my_enqueue_script" );

See my plugin T5 Admin Menu Demo for an example.

Do not define a constant PLUGIN_URL. You will run into collisions with other code.

Leave a Comment