Perché usare admin-ajax.php e come funziona?
-
-
Danotare,qualsiasi rispostaparlerebbe delmotivoper cui `temi/example/json.php` dovrebbeessere considerato una delleprincipali vulnerabilità di sicurezzaOf note, any answer would talk about why `themes/example/json.php` should be considered a major security vulnerability
- 2
- 2015-06-10
- Tom J Nowell
-
2 risposta
- voti
-
- 2015-06-10
1) Perché utilizzare
admin-ajax.php
invece di codificareiltuojsonin un file comethemes/example/json.php
e codificai tuoi dati lì?L'uso di
admin-ajax.php
significa che WordPress Core è caricatoe disponibile. Senza ciò,dovresti caricaremanualmentei file di cui haibisogno,che è unprocesso complicatoe soggetto aguasti senon conoscimolto,moltobene il Core. E quanto seibravo con la sicurezza Javascript?2) Comefunziona
admin-ajax.php
? Non ci capiscomolto file. Caricatutte lefunzioniin modo chetu siaprontoper usarle?- Caricail WordPress Core,il che significa chepuoi usare cose come
$wpdb
e$WP_Query
. Questo èfino alla riga 25. - Invia alcuneintestazioni - righe 37 - 41.
- Un'intestazione deltipo di contenuto
- Un'intestazioneperindicare aibrowser dinonmemorizzarenella cachei risultati
- Leintestazioniinteressanti sono quelleinviate da
send_nosniff_headers()
- e
nocache_headers()
.
- L'hook
admin_init
si attiva. - Le azioniprincipali sono definitee registrate dinamicamente - righe 46-73.
Questinon verranno registrati ameno chenon sianonecessari,cioè ameno che
vengono richiestitramite
$_GET
o$_POST
. - Viene attivato l'hook API "heartbeat" - riga 75
- Viene verificato lo stato di "accesso" dell'utente richiedentee ilfile viene attivato un hook amministrativo appropriato o "senzaprivilegi".
Glielementi # 1e # 6 sonoi motiviprincipaliper utilizzare l'API AJAX,amioparere. Haiil WordPress Core,di cui hai quasi certamentebisogno,e hai lo stesso sistema di sicurezza dell'accesso del resto di WordPress.
1) Why use
admin-ajax.php
instead of encoding your json in a separate file likethemes/example/json.php
and encode your data there?Using
admin-ajax.php
means that the WordPress Core is loaded and available. WIthout that, you would need to hand load the files you need, which is a complicated process and prone to failure if you don't know the Core very, very well. And, how good are you with Javascript security?2) How does
admin-ajax.php
work? I don't understand much from that file. Does it load all the functions so you are ready to use them?- It loads the WordPress Core, meaning you can use things like
$wpdb
and$WP_Query
. That is through about line 25. - It sends a few headers-- lines 37 - 41.
- A content type header
- A header to tell browsers not to cache the results
- The interesting headers are those sent by
send_nosniff_headers()
- and
nocache_headers()
.
- The
admin_init
hook fires. - Core actions are defined and registered dynamically-- lines 46 - 73.
These won't be registered unless they are needed-- that is, unless
they are requested via
$_GET
or$_POST
. - The "heartbeat" API hook fires-- line 75
- The "logged in" status of the requesting user is checked and the appropriate administrative or "no priviledge" hook is fired.
Items #1 and #6 are the primary reasons to use the AJAX API, in my opinion. You have the WordPress Core, which you almost certainly need, and you have the same login security system as with the rest of WordPress.
-
- 2015-06-10
admin-ajax.php
faparte dell ' API AJAX di WordPresse sì,gestisce le richieste sia dalbackend che dalfront. ecco cosa ho capitoper latua domanda che è:2) Comefunziona admin-ajax.php?
per la logica puoi visita qui.
Questopresuppone chetu sappiagià come accodare JavaScript,ecc.
Pezzo JavaScript:
jQuery(document).ready(function($) { // We'll pass this variable to the PHP function example_ajax_request var fruit = 'Banana'; // This does the ajax request $.ajax({ url: ajaxurl, data: { 'action':'example_ajax_request', 'fruit' : fruit }, success:function(data) { // This outputs the result of the ajax request console.log(data); }, error: function(errorThrown){ console.log(errorThrown); } }); });
Pezzo PHP:
function example_ajax_request() { // The $_REQUEST contains all the data sent via ajax if ( isset($_REQUEST) ) { $fruit = $_REQUEST['fruit']; // Let's take the data that was sent and do something with it if ( $fruit == 'Banana' ) { $fruit = 'Apple'; } // Now we'll return it to the javascript function // Anything outputted will be returned in the response echo $fruit; // If you're debugging, it might be useful to see what was sent in the $_REQUEST // print_r($_REQUEST); } // Always die in functions echoing ajax content die(); } add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' ); // If you wanted to also use the function for non-logged in users (in a theme for example) add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
1) Perché usare admin-ajax.phpinvece di codificareiltuojsonin unfile file cometemi/esempio/json.phpe codificarei tuoi dati lì?
puòessere utile. admin- ajax.php vsmodello dipaginapersonalizzatoper richieste Ajax
admin-ajax.php
is part of the WordPress AJAX API, and yes, it does handle requests from both backend and front. here what i figure-out for your question that is:2) How does admin-ajax.php work?
for the logic you can visit here.
This assumes you already know how to enqueue JavaScript, etc.
JavaScript Piece:
jQuery(document).ready(function($) { // We'll pass this variable to the PHP function example_ajax_request var fruit = 'Banana'; // This does the ajax request $.ajax({ url: ajaxurl, data: { 'action':'example_ajax_request', 'fruit' : fruit }, success:function(data) { // This outputs the result of the ajax request console.log(data); }, error: function(errorThrown){ console.log(errorThrown); } }); });
PHP Piece:
function example_ajax_request() { // The $_REQUEST contains all the data sent via ajax if ( isset($_REQUEST) ) { $fruit = $_REQUEST['fruit']; // Let's take the data that was sent and do something with it if ( $fruit == 'Banana' ) { $fruit = 'Apple'; } // Now we'll return it to the javascript function // Anything outputted will be returned in the response echo $fruit; // If you're debugging, it might be useful to see what was sent in the $_REQUEST // print_r($_REQUEST); } // Always die in functions echoing ajax content die(); } add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' ); // If you wanted to also use the function for non-logged in users (in a theme for example) add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
1) Why use admin-ajax.php instead of encoding your json in a separate file like themes/example/json.php and encode your data there?
may be this helpful.admin-ajax.php vs Custom Page Template for Ajax Requests
-
Ehi,puoi spiegare questi hook di azione 'wp_ajax_example_ajax_request'e 'wp_ajax_nopriv_example_ajax_request' Nontrovo spiegazioni danessunaparte.Inoltre cosa risolve ajaxurl?GrazieHey, can you explain these action hooks 'wp_ajax_example_ajax_request' and 'wp_ajax_nopriv_example_ajax_request' I find no explanation anywhere. Also what does ajaxurl resolve to? Thanks
- 0
- 2020-04-13
- David Okwii
Lamia chiamata ajaxperi datijsonfunzionabene in questomodo functions.php:
javascript:
Ho 2 domande.
1) Perché utilizzare admin-ajax.phpinvece di codificareiltuojsonin unfile separato come
themes/example/json.php
e codificarei tuoi dati lì?2) Comefunziona admin-ajax.php?Non capiscomolto da quelfile.Caricatutte lefunzioniin modo chetu siaprontoper usarle?
Grazie!