M
M
Michael2020-04-15 12:58:20
WordPress
Michael, 2020-04-15 12:58:20

Why write url: admin-ajax.php?

Hello!
I'm learning ajax in Wordpress.
In some examples I see this code

$.ajax({
               url: "/wp-admin/admin-ajax.php",
               method: 'post',

but at the same time they write that we write the handler in function.php
I don’t quite understand why write the url: "/wp-admin/admin-ajax.php"
if we write the handler in functions.php
What will happen if we don't write this url ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pychev Anatoly, 2020-04-15
@pton

Here is the answer to your question and to all subsequent ones on this topic.

I
Igor Vorotnev, 2020-04-16
@HeadOnFire

Your js sends data to the handler file which is the entry point. Think of it as an API endpoint . Because functions.phpyou can't send a request for a file, it's an include. But this /wp-admin/admin-ajax.phpis not an include, a slightly lighter version of the whole WordPress is raised there, which is designed solely to accept your sent data, determine which action you want to use, and through this action pass the received data to the handler that hangs on this action. And now functions.phpyou are on this action in the format wp_ajax_{$action}and wp_ajax_nopriv_{$action}connect your handler, which will accept the sent data, do everything you need with it and return the answer, which in turn /wp-admin/admin-ajax.phpwill return back to your js.
According to the link Anatoly Pychevmore details with examples. In general, open the file itself in the editor /wp-admin/admin-ajax.phpand some questions will disappear by themselves.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question