0

I have a plugin that has a top level menu and subsequent submenus. Each one of these pages are going to need their own css and javascript. My code at the moment is:

//main menu
add_menu_page( "My Business", "My Business", "contributor", "fbusiness", array($fmb_admin, "get_page"));
//sub menus
add_submenu_page("fatemagic-business", "Products", "Products", "contributor", "fbusiness-product", array($fmb_product, "get_page"));

What I need is for custom css and scripts for the page that is displayed when the submenu is clicked. I did do this before but have just spent 3hrs trying to find it online and can't. If my memory is correct it looked something like:

$page = add_submenu_page("fatemagic-business", "Products", "Products", "contributor", "fbusiness-product", array($fmb_product, "get_page"));
print_admin_script($page, "http://location/to/javascript");
print_admin_style($page, "http://location/to/css");

Maybe this technique is deprecated? It was about 3 or 4 years ago when I used this technique.

cheers ;)

1
  • ... found it. arrgggh, sorry for wasting time. Will put up the answer in 5 mins. (knew i'd find it as soon as i asked a question, lol) ans: add_action('admin_print_styles-' . $page, 'func_to_enqueue_script') - will put up detailed answer in about 5 mins
    – Coombesy
    Aug 30, 2011 at 9:02

2 Answers 2

2

Use the admin_head hook and just echo out what you need to:

function my_admin_head() {
    // Custom Style
    echo '<link rel="stylesheet" href="'.WP_PLUGIN_DIR.'/style.css" type="text/css" />';

    // Our JS
    echo '<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>';

}
add_action('admin_head', 'my_admin_head');
1
  • unfortunately this will add the styles and scripts to all admin pages. But I finally found the answer - literally 2 search's after asking the question lol
    – Coombesy
    Aug 30, 2011 at 9:12
0

Sorry for the late reply on this but here is the answer. In order to have custom css for each of my sub-menu admin pages my code needs to be like this:

    //main menu
add_menu_page( "My Business", "My Business", "contributor", "site-business", array($fmb_admin, "get_page"));

//sub menus
$page = add_submenu_page("site-business", "Products", "Products", "contributor", "site-business-product", array($fmb_product, "get_page"));
add_action('admin_print_styles-' . $page, array($fmb_product, "load_styles"));
add_action('admin_print_scripts-' . $page, array($fmb_product, "load_scripts"));

hope this helps someone else ;)

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.