625 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
ChapterThemes says

I’m using wp_localize_script to load the ajaxurl parameter if needed for a script file.

Now i have a case where there are multiple scripts using ajax, so i used wp_localize_script more than once. Resulting in multiple /* <![CDATA[ */ lines declaring the same ajaxurl.

Is there a way to avoid this ?

116 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 100+ items on Envato Market
+5 more
nagaemas says

I’m using wp_localize_script to load the ajaxurl parameter if needed for a script file.

Now i have a case where there are multiple scripts using ajax, so i used wp_localize_script more than once. Resulting in multiple /* <![CDATA[ */ lines declaring the same ajaxurl.

Is there a way to avoid this ?

Not that I know of, I’m also wondering this myself. Maybe you could create your own namespace, load there your variables and make sure that the variables is loaded when you need it :)

But maybe someone has a better idea?

625 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
ChapterThemes says

Yes you could include your own cdata rule with it at the footer or something.

But using localize is probably the best since it always has the right URL + it’s the way it’s intended to load the parameter for an ajax depended script, so i like to stay with this one

116 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 100+ items on Envato Market
+5 more
nagaemas says

Yes you could include your own cdata rule with it at the footer or something. But using localize is probably the best since it always has the right URL + it’s the way it’s intended to load the parameter for an ajax depended script, so i like to stay with this one

Yes, I meant use wp_localize_script, but do that only one time in your whole theme. You can add/remove variables using WordPress filters.

625 posts
  • Has been part of the Envato Community for over 3 years
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
ChapterThemes says


Yes you could include your own cdata rule with it at the footer or something. But using localize is probably the best since it always has the right URL + it’s the way it’s intended to load the parameter for an ajax depended script, so i like to stay with this one
Yes, I meant use wp_localize_script, but do that only one time in your whole theme. You can add/remove variables using WordPress filters.

Ah ok.

Well, my situation is i’m including a script that’s needs ajax. So i call the localize script to include the ajaxurl parameter.

Then the other one is from a plugin, which also loads a script file and localizez the scripts for ajax use, so it again puts in the same ajaxurl parameter.

Now if the theme was for my purpose only then yes, i just could make sure the ajax url is set once. But what about plugins in general, who need an ajaxurl parameter, include one for themselfs.

Now i’m talking about it it kind of makes sence, since you define your own varname for the ajaxurl. This way you know what the varname is when you set it for your plugin.

so the problem IS that you need to set your own varname. If WordPress would have used one fixed varname for the ajax url when included, there could be a check to make sure it is only placed once in the source and all plugins can use that same fixed ajaxurl..

ah well, maybe later. not going to worry about this anymore :)

116 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 100+ items on Envato Market
+5 more
nagaemas says

Just discovered something new, if you use WP 3.5, then do wp_enqueue_script( ‘media-models’), then you have a convenient wp.media.ajax available for use, and you don’t have to specify the admin ajax-url anymore.

The only drawback is that this is only compatible with WP 3.5+

1481 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+3 more
OriginalEXE says

Just discovered something new, if you use WP 3.5, then do wp_enqueue_script( ‘media-models’), then you have a convenient wp.media.ajax available for use, and you don’t have to specify the admin ajax-url anymore. The only drawback is that this is only compatible with WP 3.5+

Why would you want to support < 3.5 in your new products, just a hassle.

Everyone should upgrade to the latest WP version.

741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

I wouldn’t worry too much about the Ajax URL being printed several times on a page. It will still be faster than requesting entire file (media-models) from the server. While working with AJAX you will probably need more variables than Ajax URL for each script, so you will still use wp_localize_script on each.

116 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 100+ items on Envato Market
+5 more
nagaemas says

I wouldn’t worry too much about the Ajax URL being printed several times on a page. It will still be faster than requesting entire file (media-models) from the server. While working with AJAX you will probably need more variables than Ajax URL for each script, so you will still use wp_localize_script on each.

Actually, the media-models will be always present on the post edit screen if your post type supports editor, because it’s used by the ‘Add Media’ button. That’s the good thing :)

741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says


I wouldn’t worry too much about the Ajax URL being printed several times on a page. It will still be faster than requesting entire file (media-models) from the server. While working with AJAX you will probably need more variables than Ajax URL for each script, so you will still use wp_localize_script on each.
Actually, the media-models will be always present on the post edit screen if your post type supports editor, because it’s used by the ‘Add Media’ button. That’s the good thing :)

That’s true, but I thought we were talking about frontend Ajax scripts. :)

by
by
by
by
by
by