648 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
+1 more
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 ?

152 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
+6 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?

648 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
+1 more
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

152 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
+6 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.

648 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
+1 more
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 :)

152 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
+6 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+

1509 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
+2 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.

152 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
+6 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. :)

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by