It's not uncommon for WordPress sites to have 10+ instances of the admin-ajax.php path outputted to the front-end for use by plugins and themes. Does anyone know if there is a reason WordPress core hasn't added this as a default? It seems like it would save a ton of duplication.
-
1kinda pointless question. yes it should be, but asking here is pointless as we can only guess why. To know why, you should open ticket and request this featureMark Kaplun– Mark Kaplun2016-03-06 03:21:38 +00:00Commented Mar 6, 2016 at 3:21
-
If someone actually knows the answer to this then it isn't pointless, correct? But, to your point, I'll look at opening a ticket. I just thought there may be a reason for it that is known to someone.Jeremiah Prummer– Jeremiah Prummer2016-03-06 06:06:12 +00:00Commented Mar 6, 2016 at 6:06
-
It is just that why feature X is not in core can have many explanations, including,"no one asked for it". Even why feature Y is in core will not be a great question. a better question can probably be "how do I avoid ajax url duplication"Mark Kaplun– Mark Kaplun2016-03-06 07:30:17 +00:00Commented Mar 6, 2016 at 7:30
-
If you already know the path, why do you need it to be stored in an object? Do you store the paths to other resources such as images in JS objects as well?dotancohen– dotancohen2016-03-06 08:13:12 +00:00Commented Mar 6, 2016 at 8:13
1 Answer
As you are probably aware admin-ajax.php has been around for a very long and it provides a unified file for making AJAX requests, whether they are from the back end or front end, however its responsbility is not to provide a unified way to access it's URL as a JavaScript variable in the front end.
WordPress provides you the destination file, it's up to you how you access it.
Most people who are localizing variables in the front end do so along with other data they need for the application, not just the AJAX URL.
I suspect this, along with the fact that WordPress provides no other standard information in the way of JavaScript variables (excluding the emoji rubbish); as to being the reason why it hasn't been standardized in the front end.
I've wondered the same thing before, in the spirit of keeping it DRY, but this is WordPress afterall where there is a great deal of carry-over in old architecture and design decisions that are still yet to be improved and this could very well be one of those things.