Home » Wordpress » How do i add custom css and js files to admin area of wordpress [closed]

How do i add custom css and js files to admin area of wordpress [closed]

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

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' );