1

For my plugin I need to make a meta box for some custom fields in my CPT (xyz), and to organize the fields, I need to implement tabs. And I's taught to load jQuery/JavaScripts using wp_enqueue_script(), so from my plugin, I entered the following (with Chip Bennett's suggestion in mind, I excluded wp_register_script() and used only the wp_enqueue_script() and avoided deregistering built-in scripts):

function my_scripts() {    

    wp_enqueue_script( 'jquery-lib-scripts', plugins_url( '/js/jquery-1.10.2.min.js', __FILE__ ) );
    wp_enqueue_script( 'jquery-ui-for-tabs', plugins_url( '/js/jquery-ui.js', __FILE__ ) );
    //wp_enqueue_script( 'plugin-scripts', plugins_url( '/js/plugin-scripts.js', __FILE__ ) );

    wp_enqueue_style( 'plugin-style', plugins_url( 'plugin-style.css', __FILE__ ) );

}

add_action('admin_enqueue_scripts', 'my_scripts');

So the files are loading globally in admin panel.

Everything's fine, though I's full aware about the built-in WP admin scripts jquery, jquery-ui-core, and jquery-ui-tabs. But somehow my scripts are not conflicting, and is working with my tabs in my CPT's post-new.php (Add new) page; but is breaking the tabs in post.php (Edit screen of CPT xyz).

I am working on my dev. environment localhost using WAMP, and using WP 3.9.1, while I tried deactivating all other plugins and tried switching to default T14 theme.

Just found that, with my code the tabs are working in all pages, but if I activate any other plugin, even Akismet, it's CONFLICTING. I've learned about the noConflict wrappers, but assumed they are only for custom scripts, but how can I enqueue library scripts for my plugin in a noConflict wrapper?

  • I'm not sure that'd work: noConflict is to avoid defining $ because some other popular libraries also use $ (e.g. mootools), it's not AFAIK intended to support entirely different copies of jQuery running in parallel. Do you really need that? Can you really not just use the provided jquery and jquery-ui-tabs? They're very recent, and jQuery is 1.11.1 so newer than you're loading. – Rup Jun 26 '14 at 8:58
  • If you really did need these versions your best bet would be to paste both scripts into a new file and wrap them in code that saves the old values of $ and jQuery, loads your scripts (jQuery, UI and plugin) then restores the old versions. I can't promise that will completely not conflict (e.g. interfere with page-level events set up by the other copy) but I think it's your best hope. – Rup Jun 26 '14 at 8:59
  • I don't know WHY ON EARTH WITH ME, but I COULDN'T make it work ANYTIME doing wp_enqueue_script('jquery'). :( – Mayeenul Islam Jun 26 '14 at 13:05
0

I'm NOT actually Answering the question, I'm putting here, how I got my solution without being on that course:

First of all, I got the initial solution to the enqueue problem from WPSE answer. It's working like a charm. But the problem of conflicting re-occurred when I tried embedding the Media Uploaded to a field with wp_enqueue_media(), it's not working even in that page the Featured Image jquery uploader was working.

I tried with deactivating all my plugins, and switched back to TwentyFourteen with define('SCRIPT_DEBUG', true). And with the suggestion of @G.M. I tried the following code provided by him:

function header_sent() { headers_sent() && die('Sent!'); }
add_action( 'init', 'header_sent' );

to check whether any header is sent already, but I got the green signal. Then with multiple try, and @Rup's comment, I enqueued all the latest jQuery things (jQ 1.11.1 and jQUI 1.10.4). But still the problem resides. But with these broad effort I at least found a hope that, my plugin is not conflicting with Akismet.

I revamped the wp_enqueue_media() with this SO answer. But when I activated WordPress SEO, though my plugin jQuery is working, but the WP SEO. So it's a clear conflict with the WordPress SEO.

Then I switched the meta box's priority from 'high' to 'low'$mdash; nothing happened. They something clicked my mind, and I eneuqued my scripts in_footer with:

wp_enqueue_script( 'jquery-lib-scripts', plugins_url( '/js/jquery-1.11.1.min.js', __FILE__ ), '', '', true );
wp_enqueue_script( 'jquery-ui-for-tabs', plugins_url( '/js/jquery-ui-1.10.4.min.js', __FILE__ ), '', '', true );

That's solved my problem here.
(But I actually don't know if the scripts conflict with any other plugin in future, then what would I do then :( )

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.