How to check if the current page is a plugin admin panel in wordpress

风格不统一 提交于 2019-12-03 15:14:37

You can use the WP Screen API:

$screen = get_current_screen();

if ( in_array( $screen->id, array( 'some_admin_page', 'another_admin_page' ) ) )
{
    wp_enqueue_script( 'bridge_script' );
}

Note that you can simply register the scripts on the init or admin_enqueue_scripts hook (as you did), and enqueue them "in-page", i.e. in the callback function for add_menu_page().

brasofilo

The proper way to do it is using add_*_page and admin_print_scripts-$your_plugin_page:

add_action( 'admin_menu', 'add_page_so_20162413' );

function add_page_so_20162413()
{
    $my_page = add_menu_page( /* etc */ );
    add_action( "admin_print_scripts-$my_page", 'enqueue_so_20162413' );
}

function enqueue_so_20162413()
{
    wp_enqueue_script( /* etc */ );
    wp_enqueue_style( /* etc */ );
}

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.

you can use is_admin() to check if you are on admin panel http://codex.wordpress.org/Function_Reference/is_admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!