962 posts
  • Has been part of the Envato Community for over 6 years
  • Has sold $10,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
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
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+2 more
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
  • Has been part of the Envato Community for over 3 years
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+3 more
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
  • Has been part of the Envato Community for over 6 years
  • Has sold $10,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
EireStudio says

Thanks PrimaThemes, much much appreciated :)

894 posts
  • Has sold $1,000+ on Envato Market
  • Has referred 1+ members
  • Has collected 10+ items on Envato Market
  • Has been part of the Envato Community for over 3 years
+5 more
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
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 2 years
  • Located in Bangladesh
thememela says

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

1208 posts
  • Has been part of the Envato Community for over 4 years
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 50+ items on Envato Market
+5 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