Skip to content

Instantly share code, notes, and snippets.

@RiFi2k
Created April 12, 2017 00:12
Show Gist options
  • Save RiFi2k/907550d2f3f496d989e86f93b3c6edfd to your computer and use it in GitHub Desktop.
Save RiFi2k/907550d2f3f496d989e86f93b3c6edfd to your computer and use it in GitHub Desktop.
add_action( 'admin_menu', 'add_my_admin_menus' );
/**
* Create the administration menus in the left-hand nav and load the JavaScript conditionally only on that page
*/
function add_my_admin_menus(){
$my_page = add_menu_page( 'Page Title', 'Menu Title', MY_ADMIN_CAPABILITY, 'menu-slug', 'show_page_content' );
// Load the JS conditionally
add_action( 'load-' . $my_page, 'load_admin_js' );
}
// This function is only called when our plugin's page loads!
function load_admin_js(){
// Unfortunately we can't just enqueue our scripts here - it's too early. So register against the proper action hook to do it
add_action( 'admin_enqueue_scripts', 'enqueue_admin_js' );
}
function enqueue_admin_js(){
// Isn't it nice to use dependencies and the already registered core js files?
wp_enqueue_script( 'my-script', INCLUDES_URI . '/js/my_script.js', array( 'jquery-ui-core', 'jquery-ui-tabs' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment