576 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
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 ?

114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Microlancer Beta Tester
  • Has been a member for 2-3 years
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?

576 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
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

114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Microlancer Beta Tester
  • Has been a member for 2-3 years
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.

576 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 10 000 and 50 000 dollars
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 :)

114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Microlancer Beta Tester
  • Has been a member for 2-3 years
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+

1397 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
  • Croatia
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
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
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.

114 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Bought between 100 and 499 items
  • Referred between 50 and 99 users
  • Most Wanted Bounty Winner
  • Microlancer Beta Tester
  • Has been a member for 2-3 years
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
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
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