962 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Ireland
  • Sold between 10 000 and 50 000 dollars
EireStudio says

Hey Guys,

My little theme options page is coming along nicely and really happy with it so far but I want to make it more efficient and not have my JavaScripts being included on every admin page.

My code is

if(is_admin())
{
      add_action('init', 'eirestudio_scripts');
}

How would I go about including these JavaScripts just on my theme options page.

I suppose I could query the url and check for a certain string but this method seems “hacky”... Must be a better solution?

147 posts
  • Bought between 10 and 49 items
  • Elite Author
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 10 and 49 users
  • Sold between 250 000 and 1 000 000 dollars
FlexiPress says

you can do it in “wordpress” way :D

you can create global variable to make your theme settings page hookable..

add_action( 'admin_menu', 'prima_theme_settings_init' );
function prima_theme_settings_init() {
    global $primathemes_pagehook;
    $primathemes_pagehook = add_theme_page( 'Theme Settings', 'Theme Settings', 'edit_theme_options', 'primathemes', 'prima_theme_settings_page' );
    add_action( "load-{$primathemes_pagehook}", 'prima_theme_settings_enqueue_script' );
}
function prima_theme_settings_page() {
    // your theme settings page content here
}
function prima_theme_settings_enqueue_script() {
    wp_enqueue_script('prima-admin', get_template_directory_uri() . '/js/admin.js', array('jquery'), '0.1', FALSE);
}

note: for example, we use add_theme_page function to create the theme settings page… you can also use add_menu_page or add_submenu_page in similar way… ;)

80 posts
  • Bought between 1 and 9 items
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
  • United Kingdom
dpthemes says

You could forgo the wp_enqueue and just add the script tag in the source of the theme options. Enqueue is used so that multiple versions of scripts aren’t loaded, with a custom script it shouldn’t really matter

962 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Ireland
  • Sold between 10 000 and 50 000 dollars
EireStudio says

Thanks PrimaThemes, much much appreciated :)

894 posts
  • Europe
  • Has been a member for 3-4 years
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Grew a moustache for the Envato Movember competition
infuse01 says

nice read about this topic – had it bookmarked a while ago – maybe it helps:

http://www.studiograsshopper.ch/code-snippets/wordpress-current_screen-variable-admin-screens/

1 post
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Bangladesh
thememela says

You can also check this article for enqueue script into specific admin page. hope it will help.

1074 posts
  • Forum Superstar
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Author had a Free File of the Month
  • Bought between 50 and 99 items
  • Referred between 50 and 99 users
  • Europe
  • Has been a member for 3-4 years
  • Exclusive Author
+1 more
Smartik says
Check for page parameter, it should always be unique for a theme options panel in a menu section. Ex in themes section: themes.php?page=my_panel_id
if($_GET['page'] == 'my_panel_id'){
    //enqueue my scripts
}

Nothing bad or good, your script is loaded in your page. You don’t have to worry about other pages. ;)

by
by
by
by
by
by