well, that seems to be hard, no? π
just to clarify; the combinations I tried and which did not work were:
- options-general.php
- wp-youtube-lyte/options.php
- wp-youtube-lyte
- options-general.php?page=wp-youtube-lyte/options.php
Anyone got an idea of what I’m doing wrong here?
For pages that are subpages of the options section, hooks are prefixed with settings_page_, so your action will need to look something like this..
add_action( "admin_print_scripts-settings_page_YOURPAGENAME", 'my_admin_scripts' );
Hope that helps… π
thanks mark!
admin_print_scripts-settings_page_wp-youtube-lyte
admin_print_scripts-settings_page_wp-youtube-lyte/options.php
do not seem to work however. how is the pagename defined?
It’s defined when you add the page using add_options_page.. (fourth parameter – fifth parameter if you use add_submenu_page)..
Can i see the code you use to register the pages?
sure;
add_options_page( 'WP YouTube Lyte settings', 'WP YouTube Lyte', 'manage_options', __FILE__, 'lyte_settings_page');
so that would make my pagename ‘lyte_settings_page’?
The name you put into the fourth parameter is the hook you append to your admin_print_scripts actions..
So your two pieces of code should look like this..
add_options_page( 'WP YouTube Lyte settings', 'WP YouTube Lyte', 'manage_options', 'lyte_settings_page', 'lyte_settings_page');
and..
add_action( "admin_print_scripts-settings_page_lyte_settings_page", 'my_admin_scripts' );
An easier way is to store the hook in a variable for easy reference, by adding your script action inside the function you’re hooking onto admin menu, so you can do this…
$hook = add_options_page( 'WP YouTube Lyte settings', 'WP YouTube Lyte', 'manage_options', 'lyte_settings_page', 'lyte_settings_page');
add_action( 'admin_print_scripts-' . $hook, 'my_admin_scripts' );
Hope that helps…
great, thanks a million mark! is this as far as you know documented somewhere in the codex?
You’re welcome.. π
You can find a similar example on the action reference page.
http://codex.ww.wp.xz.cn/Plugin_API/Action_Reference
I needed to add some scripts and css to the users profile page and used this code and thought I’d share in case someone else needs clarification…
This registers a javascript and css file and uses these files on the user’s profile page and only loads on the user’s profile page.
add_action('admin_init','my_cool_options');
add_action('admin_print_scripts-profile.php', 'my_cool_styles');
function my_cool_options() {
wp_register_script( 'mycoolscript', getContentURL() . '/my_cool.js' );
wp_register_style( 'mycoolstyle', getContentURL() . '/my_cool.css' );
}
function my_cool_styles() {
wp_enqueue_script( 'mycoolscript' );
wp_enqueue_style( 'mycoolstyle' );
}
function getContentURL() {
$mupluginsurl = WP_CONTENT_URL."/mu-plugins";
return ($_SERVER['HTTPS'] != "on") ? $mupluginsurl : str_replace("http", "https", $mupluginsurl);
}
I used a custom function for calling the mu-plugins directory as this is where I place the files for a multi-site environment.
Hope this helps someone.