I mean is_admin() is to check if you are on the administration so it
will return false, so the code inside won´t be executed on the
front-end, won´t it ?
When you send an AJAX request using this method, you are sending it to wp-admin/admin-ajax.php
, which is in the admin. This is explained in the text you’ve quoted yourself:
Both front-end and back-end Ajax requests use admin-ajax.php so
is_admin() will always return true in your action handling code.
It’s irrelevant where the AJAX request was sent from. When the request is made it is a completely separate request to the original front-end request that loaded whatever page you were on. It’s a new request to the WordPress admin.
The important thing to remember is that an AJAX request is really just JavaScript opening a web page in the background. So it’s the same as if you opened /wp-admin/admin-ajax.php in a new tab. If you did that then is_admin()
would be true, which is why it’s still true when you make that request with JavaScript.
The other thing to remember is that add_action()
calls queue up functions to run only for the current request. Those add_action()
calls are run every time a page is loaded, getting stuff ready for the rest of the page load. When you load a front end page with your code, it queues up my_frontend_action()
to run if the current request is an AJAX request, but it’s not, so nothing happens. Nothing is stored or remembered for a later AJAX request made from that page.
When the AJAX request is made, this condition is checked again, but add_action()
is never run because is_admin()
is true, so nothing happens, and because the add_action()
from the front-end request is not stored or remembered, it doesn’t fire this time either.