0

I have decided to pull duplicated javascript from multiple custom post type files into one 'admin.min.js' file for use in all admin pages. So far I have got the script to load. This is in my scripts.php (I use Roots theme)

// Add the admin css
function custom_admin_style(){
        wp_register_style('admin_css', get_template_directory_uri() . '/assets/css/admin.min.css');
        wp_register_script('admin_js', get_template_directory_uri() . '/assets/js/admin.min.js');

        wp_enqueue_style('admin_css');
        wp_enqueue_script('admin_js');
}
add_action('admin_enqueue_scripts', 'custom_admin_style');

However the scripts rely on the TinyMCE JS plugin bundled with WordPress, so I assume this script is getting called before the TinyMCE JS plugin. What is the correct way of loading the TinyMCE plugin before this, or my script afterwards?

1

You will need to set the third parameter of wp_register_script(), which is the dependency parameter, $deps.

In your code, this is currently set to false, which should actually just be left out or set to an empty array, array() if not needed. In your case, you will set this with the handle of the script you would wish to make it depended on. You can add more than one handle here if you need to make this depended on more than one script

The handle for the tiny mce script shipped with wordpress is tiny_mce, you you can try the following:

wp_register_script('admin_js', get_template_directory_uri() . '/assets/js/admin.min.js', array( 'tiny_mce' ) );

This will ensure that your script will not load before the tine mce script is loaded. Just a note here, make sure that your handles is uniquely named as this will cause issues if there is another script loaded with the same handle

EDIT

This new issue that you are facing now is that the tiny mce script is not loaded on every page, that is how it should be. Scripts and styles should not be loaded if not needed on a page.

This is the big issue with combining scripts, and you will most probably now be loading scripts and styles that is not needed on some pages. Setting a dependency will also not work, because, as you have seen, on pages where the tiny mce script is not loading, your custom script will not load.

The only other means to accomplish this is to remove the dependency and then use priority on your action, something like this

function custom_admin_style(){
        wp_register_style('admin_css', get_template_directory_uri() . '/assets/css/admin.min.css');
        wp_register_script('admin_js', get_template_directory_uri() . '/assets/js/admin.min.js');

        wp_enqueue_style('admin_css');
        wp_enqueue_script('admin_js');
}
add_action( 'admin_enqueue_scripts', 'custom_admin_style', PHP_INT_MAX );
4
  • Issue now is that WP cannot find the functions within the js file when I pass array('tiny_mce') to wp_register_script. Do I have to explicitly call tiny MCE on each admin page first or something?
    – myol
    Jan 19 '15 at 12:56
  • See my update. Hope this helps Jan 19 '15 at 13:54
  • Thank-you for clarifying that issue. I am loading the scripts file depending on the CPT now and have the desired functionality working thanks to your answer.
    – myol
    Jan 19 '15 at 14:09
  • My pleasure, enjoy. :-) Jan 19 '15 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.