[wp-hackers] Adding javascript code after script is enqueued
Tynan Colin Beatty
junsuijin at gmail.com
Tue Sep 15 19:04:55 UTC 2009
I just tested this approach successfully for the head situation:
{{{
function my_jquery_test() {
global $wp_scripts;
if ( is_a( $wp_scripts, 'WP_Scripts' ) && in_array( 'jquery',
$wp_scripts->queue ) ) {
if ( $wp_scripts->registered['jquery']->extra['group'] == 1 ) {
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
add_action( 'wp_footer', 'my_print_footer_scripts' );
}
else {
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
add_action( 'wp_head', 'my_print_head_scripts', 9 );
}
}
}
add_action( 'wp', 'my_jquery_test', 0 );
function my_print_head_scripts() {
$rest = my_print_scripts_prep();
wp_print_head_scripts();
my_print_scripts_finish( $rest );
wp_print_head_scripts();
}
function my_print_footer_scripts() {
$rest = my_print_scripts_prep();
wp_print_footer_scripts();
my_print_scripts_finish( $rest );
wp_print_footer_scripts();
}
function my_print_scripts_prep() {
global $wp_scripts;
$offset = array_search( 'jquery', $wp_scripts->queue );
$rest = array_slice( $wp_scripts->queue, $offset );
$wp_scripts->queue = array_slice( $wp_scripts->queue, 0, $offset + 1
);
$wp_scripts->print_html = "<script
type='text/javascript'>jQuery.noConflict();</script>\n";
return $rest;
}
function my_print_scripts_finish( $array ) {
global $wp_scripts;
$wp_scripts->queue = $array;
}
}}}
This method is the easiest way i could think of to get the noconflict to
load directly after the jquery script (in case other scripts that depend
upon it are also enqueued for the same spot). The hooks provided in this
process do not allow for any other simpler approaches i could think up at
this time.
Another possible approach is to create your own version of the
wp-admin/load-scripts.php script concatenator.
peace~
On Mon, Sep 14, 2009 at 10:37 PM, Milan Dinić <liste at srpski.biz> wrote:
> Is there way to add javascript code immediately after script is enqueued? I
> found function wp_localize_script, but it adds code before script
> enqueuing.
>
> I am asking this because of following problem. jQuery file included in
> WordPress is different from official one in that it has jQuery.noConflict
> at
> it end. If we use jQuery from Google AJAX Libraries, there is noconflict so
> we need to add it.
>
> Author of plugin Use Google Libraries (which enables use of files from
> Google’s servers) solved this by enqueuing file jQnc.js (which whole
> content
> is jQuery.noConflict(); ) . It is lightweight but it needs another request
> and headers could be up to kilobyte.
>
> It would be better if noConflict code is added in html of page, it will not
> increase page size, but it will remove one request. I asked Jason could he
> enable this but he said that he is not aware of way to do this with WP API.
>
> So, does anyone knows solution for this?
>
> Thanks
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list