3

The question is asked too many times and there are too many different questions and for some reason it seems that i can't get it to work with none of the answers i have found. The very strange thing is that i had developed themes earlier and it was working.

I need to make some options to my theme, so i made separated menu in wordpress admin area and it works, i added some options to that page and it's shown, also saving options works.

Now i wanted to make a bit more options and make them tabbed with jquery ui tabs. I know that wordpress natively supports jquery ui now but it needs to be called additionally to load.

So after messing too many with code i at the end ended pulling a code from generatewp.com site which should work, but it don't, why i can't understand that.

The current code now is:

function custom_styles() {

wp_register_style( 'jquery-ui-style', 'http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css', false, false );

}

// Hook into the 'admin_enqueue_scripts' action
add_action( 'admin_enqueue_scripts', 'custom_styles' );


// Register Script
function custom_scripts() {

wp_register_script( 'jquery-ui-core', '', array( 'jquery' ), false, false );

wp_register_script( 'jquery-ui-tabs', '', array( 'jquery' ), false, false );

}

// Hook into the 'admin_enqueue_scripts' action
add_action( 'admin_enqueue_scripts', 'custom_scripts' );

According to wordpress records this should register jquery ui tabs and style but it doesn't.

I tried many other combinations and it simply doesn't work. Why i can't understand that.

lonerunner
  • 1,282
  • 6
  • 31
  • 70
  • http://css-tricks.com/snippets/wordpress/apply-custom-css-to-admin-area/ – Davit Aug 31 '13 at 23:01
  • http://stackoverflow.com/questions/3326967/how-to-add-custom-javascript-to-wordpress-admin – Davit Aug 31 '13 at 23:02
  • 5
    This question appears to be off-topic because it is about a support request for tailoring wordpress. It is explained not only in the product documentation of wordpress, there is also a Q&A site dedicated to Wordpress alone. – hakre Aug 31 '13 at 23:07
  • It doesn't work. tried that also, but it works with scripts on functions.php page, so if i have small custom portion of javascript i should include it with admin_head but not if script is in separated js file, that i should first register, than enqueue as i understood. – lonerunner Aug 31 '13 at 23:11
  • @AleksandarĐorđević you're right, i see only registration function i don't see enqueue. Try adding `wp_enqueue_script( 'jquery-ui-core' );` and `wp_enqueue_script( 'jquery-ui-tabs' );` functions inside the script hook. It could Help you to get these things to work. – iEmanuele Aug 31 '13 at 23:19
  • Do the same for your style with `wp_enqueue_style( 'jquery-ui-style' );` – iEmanuele Aug 31 '13 at 23:23
  • Yea but it still doesn't work for some unknown reasons, and i damn checked for spelling and commas to make sure i did all right, as i know you are right the script needs first register, than enqueue it and add_action to return it, the hook admin_print_styles worked for style, but admin_print_scripts doesn't work, and i tried wp_enqueue_scripts, admin_enqueue_scripts, admin_head, init none, of them worked, ill try now to deregister first and register it again. – lonerunner Aug 31 '13 at 23:41
  • And also, jquery-ui doesn't need to be registered first as it's part of core now, don't know why website suggested it, all core jquery scripts need only to enqueue it per need, problem is it won't print on the page damn it. – lonerunner Aug 31 '13 at 23:44
  • check this answer http://stackoverflow.com/a/38050537/1153703 – Bikesh M Jun 27 '16 at 09:40

1 Answers1

1

Straight from the codex @admin_enqueue_scripts There are specific hooks to do what you're trying to do.

function my_enqueue($hook) {
    if( 'edit.php' != $hook )
        return;
    wp_enqueue_script( 'my_custom_script', plugins_url('/myscript.js', __FILE__) );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );


function load_custom_wp_admin_style() {
        wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
        wp_enqueue_style( 'custom_wp_admin_css' );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
Ben Racicot
  • 5,332
  • 12
  • 66
  • 130
  • The hook is used only in case if you target specific page, you can use it without a hook also but it will load on all pages, i have read the codex, so per codex if in function i use wp_enqueue_scripts('jquery-ui') and than activate it with add_action('admin_enqueue_scripts', 'my_enqueue') this should work, but it doesn't – lonerunner Sep 01 '13 at 00:06
  • What is the console saying? Anything specific and which JS files get loaded? – Ben Racicot Sep 01 '13 at 00:10
  • It doesn't say anything wrong, i don't get any js errors, wordpress is fresh install and i don't have any other code in my theme except this what i try to include, it's just like the action hook is not even there in functions.php file. – lonerunner Sep 01 '13 at 00:42
  • Hmm please keep me posted, I'm stumped! – Ben Racicot Sep 01 '13 at 01:20
  • Spending so many hours in debugging something for what no one can get explanation when simple 5 minute re-installation can fix it is so frustrating. It looks like something was wrong with my installation of wordpress, either it wasn't completed right, or some file was missing or wasn't completed during copying wordpress on server. I just reinstalled wordpress with fresh install, and copied theme files with what i did so far and it worked. – lonerunner Sep 01 '13 at 10:23
  • Awe man, been there. Maybe one problem with a framework like this u just can't keep track of the core all the time? Glad it worked out – Ben Racicot Sep 01 '13 at 19:21