Please I want to know how to check if I’m currently in admin page of a plugin.
I’ve created a plugin with a menu item which displays a page containing some stats of this plugin use, for that, I’m using custom JQuery plugins, some CSS, which I will never use outside of this page.
So I wonder to know how can I check this, to enqueue or not plugin’s styles and JSs .
Here is my enqueue style code
function bridge_style_enqueuer() {
wp_register_style( "bridge_display_style", WP_PLUGIN_URL.'/symfony-bridge/chosen.css');
wp_register_style( "bridge_display_style_tb", WP_PLUGIN_URL.'/symfony-bridge/bootstrap.min.css');
wp_enqueue_style( 'bridge_display_style' );
wp_enqueue_style( 'bridge_display_style_tb' );
}
add_action( 'admin_init', 'bridge_style_enqueuer' );
I do the same with Js
function bridge_script_enqueuer() {
wp_register_script( "bridge_script", WP_PLUGIN_URL.'/symfony-bridge/bridge.js', array('jquery'),FASLE, TRUE);
wp_register_script( "bridge_chosen_script", WP_PLUGIN_URL.'/symfony-bridge/chosen.js', array('jquery'),FASLE, TRUE);
wp_register_script( "bridge_chosen_script_tb", WP_PLUGIN_URL.'/symfony-bridge/bootstrap.min.js', array('jquery'),FASLE, TRUE);
wp_enqueue_script( 'bridge_script' );
wp_enqueue_script( 'bridge_chosen_script' );
wp_enqueue_script( 'bridge_chosen_script_tb' );
}
add_action( 'admin_init', 'bridge_script_enqueuer' );
You can use the WP Screen API:
Note that you can simply register the scripts on the
init
oradmin_enqueue_scripts
hook (as you did), and enqueue them “in-page”, i.e. in the callback function foradd_menu_page()
.The proper way to do it is using
add_*_page
andadmin_print_scripts-$your_plugin_page
:Diggy’s suggestion is the best one when we’re having problems enqueuing with this method.
You have to just add js by the hook
admin_enqueue_scripts
— http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts.And, by the way, actually for adding scripts and css’s you should to use hook
wp_enqueue_scripts
.