Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Deregister Scripts - Wordpress Jquery Mobile WordPress

  • SOLVED

Hello,

I'm using a jquery mobile frameworked wordpress theme. Developed by Francis Robert a french developer. You can download and install the theme here http://frobert.com/en/2011/03/30/generic-jquery-mobile-wordpress-theme/

You also need to run this plugin - http://wordpress.org/extend/plugins/wp-mobile-detector/ this redirects mobile devices to the jquery mobile wordpress theme - this means you don't actually change your current theme.

My question is, because I have a 'non-device' theme with lots of wordpress plugin's present - and even though they are not present in my mobile wordpress theme, they are still being registered in the head.

See picture attached of the NET results for my mobile theme loading them in... I have coloured the scripts/css I want to keep in <strong>GREEN</strong> and coloured the unwanted scripts/css in <strong>RED</strong>.

So I basically need to deregister all the ones marked in <strong>RED</strong>.

I only need to be show the method in how to do one or multiple ones, and I can figure out the rest.



Francis Robert did actually supply a 'deregister script' located in the 'inc/preferences.php' file.

It looks like this...


// If you wish to deregister some plugin script loading, put the script names in the following array.
// The goal is to make the loading time as short as possible by cutting off unnecessary file loading.
// Example: array('shutter','thickbox','jquery-cycle','ngg-slideshow','ngg-script');
$deregister_scripts = array(); // If you wish to deregister scripts for mobile display

// If you wish to deregister some css file loading, put the names in the following array.
// Example: array('NextGEN','thickbox','thickbox');
$deregister_styles = array(); // If you wish to deregister css file loading


But i've tried arraying everything, but he has not explained how to array them, i've arrayed them as he has explained but it's not working for me, they all still load in the wp_head.

So if there's another manual way of executing this or if someone can show me what I need to array, then my question will be resolved.

Thanks in advance.

Answers (2)

2011-08-10

Daryl Lozupone answers:

Josh--

The deregister_scripts function works by passing the handle for the script as the parameter.

For example, WordPress registers the jQuery script with the handle 'jQuery'. So that would go into the array.

Based on the screenshot you have attached, it seems there are plugins adding javascript to your page. You will therefore have to look through the code for those plugins, find the line that registers the script in order to determine the handle, and then add them to the array in the code listed above.

You will be looking for a line similar to this:


wp_register_script( 'fancybox', plugins_url( 'jquery.fancybox.js' ), array( 'jQuery' ) )


or


wp_enqueue_script( 'fancybox', plugins_url( 'jquery.fancybox.js' ), array( 'jQuery' ) )


In the above examples, the handle is 'fancybox', so that is what you add to the array as mentioned above.

The same thing applies to styles, but they use the functions wp_register_style and wp_enqueue_style. These functions use the same format, however.

I hope that helps.


Josh Cranwell comments:

Thank you - this helped me understand this better.

But the problem I found once you deregister scripts and css - for example with the fancybox, the functions are still in place, which means you get a javascript error.

I think these are know as actions - but I can't figure out how to deregister these now.


Just a quick note, do you use gravityforms? That's the only plugin I can't find a handle for the script or css.

Thanks your help.

2011-08-10

Christianto answers:

Hi,

Where you put your array value?
$deregister_scripts = array('jquery', 'json2');
the variable should be define in jquery mobile theme functions.php

Also check the name supply in the array, it should match the name of register script on your 'no-mobile' wordpress theme.

try to put this code in you mobile theme functions.php, please check if jquery and json aren't loaded.

$deregister_scripts = array('jquery', 'json2');
function deregister_mobile() {
global $deregister_scripts;
if(!is_admin()){
foreach ($deregister_scripts as $deregit) {
wp_deregister_script($deregit);
}
}
}
add_action('init', 'deregister_mobile');


There another thing, if the script isn't loaded using wp_enqueue_script and register using wp_register_script in other word its being output directly. It won't work.


Josh Cranwell comments:

Thanks your response.

This has helped me understand how to add this in the functions manually which will be great help in the future.

Sorry, I'm still trying to get to grips with this site, I think I voted wrong and I can't re-vote now. I marked you 1 - is that good or bad?

Thanks for your help.