Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#31785 closed defect (bug) (invalid)

admin_footer

Reported by: mikayel's profile Mikayel Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1.1
Component: Administration Keywords:
Focuses: Cc:

Description

It is not possible to to insert my script right before </body>

In my case in Dojo require is conflicting with jQuery require.
I just need to insert Dojo code after jQuery load.

add_action('admin_footer', 'my_admin_footer_function', 100); is not helping

Is it possible to add new hook for this case or change admin_footer?


Attachments (3)

hellodojo.php (831 bytes) - added by Mikayel 8 years ago.
Simple Plugin for Dojo Bug
Screen Shot 2015-03-28 at 07.35.37.png (118.2 KB) - added by MattyRob 8 years ago.
Screen Shot 2015-03-28 at 07.36.55.png (289.6 KB) - added by MattyRob 8 years ago.

Download all attachments as: .zip

Change History (19)

#1 follow-up: @MattyRob
8 years ago

Have you tried using the 'wp_enqueue_scripts' hook instead?

https://codex.wordpress.org/Function_Reference/wp_enqueue_script

#2 in reply to: ↑ 1 @Mikayel
8 years ago

Replying to MattyRob:

Have you tried using the 'wp_enqueue_scripts' hook instead?

https://codex.wordpress.org/Function_Reference/wp_enqueue_script

I did, it is not helping.

#3 @MattyRob
8 years ago

Does manually adding the script before the body resolve the conflict? Can you post some code examples and the javascript errors?

Last edited 8 years ago by MattyRob (previous) (diff)

#4 @Mikayel
8 years ago

here you go

function my_custom_js() {
	if (!is_curent_plugin()) {
		return;
	}

echo  ' <script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script> ';

echo  '<script type="text/javascript">
<!--	
	dojo.require("dojox.validate");
	dojo.require("dojox.validate.web");
	dojo.require("dojox.validate.us");
	dojo.require("dojox.validate.check");	
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.ValidationTextBox");
	dojo.require("dijit.form.NumberTextBox");
	dojo.require("dijit.form.ComboBox");
	dojo.require("dijit.form.DateTextBox");	
	dojo.require("dijit.dijit");
	dojo.require("dojo.parser");
	
	dojo.addOnLoad(function(){
		dojo.addClass(document.body, "tundra");
	});	
//--></script>
';
}
add_action('admin_footer', 'my_custom_js', 100)

;

When I am calling my_custom_js directly from wp-includs/admin-footer.php javascript is working, but this is just hack, I want to have proper solution.

#5 follow-up: @MattyRob
8 years ago

I'm presuming this javascript is supposed to add a class to the body tag - right?

If I paste the code directly into the wp-admin/admin-footer.php file, that class does not get added for me.

I can see the javascript in the page source and there are no console errors, the same applies if I load this via a plugin.

#6 in reply to: ↑ 5 @Mikayel
8 years ago

Replying to MattyRob:

I'm presuming this javascript is supposed to add a class to the body tag - right?

If I paste the code directly into the wp-admin/admin-footer.php file, that class does not get added for me.

I can see the javascript in the page source and there are no console errors, the same applies if I load this via a plugin.

Adding class to body can be added by jQuery, it is not the problem, the problem is dojo dijit that I am using in different custom plugins for different clients.

if you remove flowing code from my code it will trow erros in most cases

if (!is_curent_plugin()) {
		return;
	}

If you add the flowing code in any plugin you can see the error when you are in wp-admin.

function my_custom_js() {
echo  ' <script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script> ';

echo  '<script type="text/javascript">
<!--	
	dojo.require("dojox.validate");
	dojo.require("dojox.validate.web");
	dojo.require("dojox.validate.us");
	dojo.require("dojox.validate.check");	
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.ValidationTextBox");
	dojo.require("dijit.form.NumberTextBox");
	dojo.require("dijit.form.ComboBox");
	dojo.require("dijit.form.DateTextBox");	
	dojo.require("dijit.dijit");
	dojo.require("dojo.parser");
	
	dojo.addOnLoad(function(){
		alert("Hi Dojo");
	});	
//--></script>
';
}
add_action('admin_footer', 'my_custom_js', 100);


#7 follow-up: @MattyRob
8 years ago

I already removed that plugin check and put he following code into the wp-admin/admin-footer.php file. It made the 'alert', and threw no errors. This is on a fresh install with no current active plugins.

echo  ' <script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script> ';

echo  '<script type="text/javascript">
<!--
	dojo.require("dojox.validate");
	dojo.require("dojox.validate.web");
	dojo.require("dojox.validate.us");
	dojo.require("dojox.validate.check");
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.ValidationTextBox");
	dojo.require("dijit.form.NumberTextBox");
	dojo.require("dijit.form.ComboBox");
	dojo.require("dijit.form.DateTextBox");
	dojo.require("dijit.dijit");
	dojo.require("dojo.parser");

	dojo.addOnLoad(function(){
		alert( "HERE" );
		dojo.addClass( "wpwrap", "tundra");
	});
//--></script>';

#8 in reply to: ↑ 7 @Mikayel
8 years ago

Problem is that I can't touch admin-footer.php when I am creating plugin, I have to use hook, but when you try hook in plugin it will not work.

Replying to MattyRob:

I already removed that plugin check and put he following code into the wp-admin/admin-footer.php file. It made the 'alert', and threw no errors. This is on a fresh install with no current active plugins.

echo  ' <script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script> ';

echo  '<script type="text/javascript">
<!--
	dojo.require("dojox.validate");
	dojo.require("dojox.validate.web");
	dojo.require("dojox.validate.us");
	dojo.require("dojox.validate.check");
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.ValidationTextBox");
	dojo.require("dijit.form.NumberTextBox");
	dojo.require("dijit.form.ComboBox");
	dojo.require("dijit.form.DateTextBox");
	dojo.require("dijit.dijit");
	dojo.require("dojo.parser");

	dojo.addOnLoad(function(){
		alert( "HERE" );
		dojo.addClass( "wpwrap", "tundra");
	});
//--></script>';

#9 follow-up: @MattyRob
8 years ago

The following in an active plugin file produced the same "HERE" alert for me. Can you disable all other plugins and use a core theme. Does that work any better?

function my_custom_js() {
echo  '<script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script>';

echo '<script type="text/javascript">
<!--
	dojo.require("dojox.validate");
	dojo.require("dojox.validate.web");
	dojo.require("dojox.validate.us");
	dojo.require("dojox.validate.check");
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.ValidationTextBox");
	dojo.require("dijit.form.NumberTextBox");
	dojo.require("dijit.form.ComboBox");
	dojo.require("dijit.form.DateTextBox");
	dojo.require("dijit.dijit");
	dojo.require("dojo.parser");

	dojo.addOnLoad(function(){
		alert( "HERE" );
		dojo.addClass( "wpwrap", "tundra");
	});
//--></script>';
}
add_action('admin_footer', 'my_custom_js', 100);

@Mikayel
8 years ago

Simple Plugin for Dojo Bug

#10 in reply to: ↑ 9 ; follow-up: @Mikayel
8 years ago

Replying to MattyRob:

The following in an active plugin file produced the same "HERE" alert for me. Can you disable all other plugins and use a core theme. Does that work any better?

function my_custom_js() {
echo  '<script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.0/dojo/dojo.js" data-dojo-config="parseOnLoad: true"></script>';

echo '<script type="text/javascript">
<!--
	dojo.require("dojox.validate");
	dojo.require("dojox.validate.web");
	dojo.require("dojox.validate.us");
	dojo.require("dojox.validate.check");
	dojo.require("dijit.form.TextBox");
	dojo.require("dijit.form.ValidationTextBox");
	dojo.require("dijit.form.NumberTextBox");
	dojo.require("dijit.form.ComboBox");
	dojo.require("dijit.form.DateTextBox");
	dojo.require("dijit.dijit");
	dojo.require("dojo.parser");

	dojo.addOnLoad(function(){
		alert( "HERE" );
		dojo.addClass( "wpwrap", "tundra");
	});
//--></script>';
}
add_action('admin_footer', 'my_custom_js', 100);

Please find attached hellodojo.php plugin, when you install plugin and open the dashboard tab (/wp-admin/index.php), you can see errors in console.

I need to place my code after wpOnload(), and everything will be OK.

<script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
</body>

#11 follow-up: @MattyRob
8 years ago

I downloaded your plugin and installed it, screenshots attached above - seems to work fine for me.

#12 in reply to: ↑ 10 @SergeyBiryukov
8 years ago

Replying to Mikayel:

Please find attached hellodojo.php plugin, when you install plugin and open the dashboard tab (/wp-admin/index.php), you can see errors in console.

Could not reproduce on a clean install, works as expected for me.

#13 in reply to: ↑ 11 @Mikayel
8 years ago

Replying to MattyRob:

I downloaded your plugin and installed it, screenshots attached above - seems to work fine for me.

Can you please go to Dashboard or Media Library page? You are in Plugins page.

And it is clean install WP 4.1.1

#14 follow-up: @MattyRob
8 years ago

  • Resolution set to invalid
  • Status changed from new to closed

The error seems to be because dojo is trying to load jQuery from the same CDN but map-forming the URL, I suspect that you see it working if you manually force dojo to load after jQuery because jQuery is detected already and not from a 404 URI.

A fix that works for me is to use a slightly later hook:

add_action('admin_print_footer_scripts', 'my_custom_js', 100);

I suspect this is the hook used to load footer javascript libraries so hooking to that with a lower priority works fine in my testing.

#15 in reply to: ↑ 14 @Mikayel
8 years ago

Replying to MattyRob:

The error seems to be because dojo is trying to load jQuery from the same CDN but map-forming the URL, I suspect that you see it working if you manually force dojo to load after jQuery because jQuery is detected already and not from a 404 URI.

A fix that works for me is to use a slightly later hook:

add_action('admin_print_footer_scripts', 'my_custom_js', 100);

I suspect this is the hook used to load footer javascript libraries so hooking to that with a lower priority works fine in my testing.

Thank you very much! This is working well!

#16 @SergeyBiryukov
8 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.