URL Ajax di Wordpress per la funzione in functions.php
3 risposta
- voti
-
- 2016-10-11
//ajax call var data = { action: 'folder_contents', path: datafolder }; var ajaxurl = baseurl + ''; //WHAT IS THIS?!?! jQuery.post(ajaxurl, data, function(response) { alert('Got this from the server: ' + response); console.log(response); });
Eiltuo PHPin functions.php
//Called by Ajax from App - list the contents of the folder so they can be downloaded and stored locally function folder_contents() { $folderPath = $_POST['path'] ; $files = array_diff(scandir($path), array('.', '..')); print_r($files); return $files; die(); } add_action('wp_ajax_folder_contents', 'folder_contents'); add_action('wp_ajax_nopriv_folder_contents', 'folder_contents');
Quanto sopra aggiunge latuafunzione come azione Ajax. Quindi chiamiilnome dellafunzioneneltuo AJAX.
Vedi https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_ (azione)
//ajax call var data = { action: 'folder_contents', path: datafolder }; var ajaxurl = baseurl + ''; //WHAT IS THIS?!?! jQuery.post(ajaxurl, data, function(response) { alert('Got this from the server: ' + response); console.log(response); });
And your PHP in functions.php
//Called by Ajax from App - list the contents of the folder so they can be downloaded and stored locally function folder_contents() { $folderPath = $_POST['path'] ; $files = array_diff(scandir($path), array('.', '..')); print_r($files); return $files; die(); } add_action('wp_ajax_folder_contents', 'folder_contents'); add_action('wp_ajax_nopriv_folder_contents', 'folder_contents');
The above add your function as an ajax action. You then call the function name in your AJAX.
See https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)
-
Spiacenti,neltuo JS devi anche aggiungere l'ajaxurlperiltuo sito.Ilmodoin cui lofaccio è definendoloe localizzando lo scriptper l'accesso.Vedi: https://codex.wordpress.org/Function_Reference/wp_localize_scriptSorry, in your JS you also need to add the ajaxurl for your site. The way i do this is by defining it and localizing the script for access. See: https://codex.wordpress.org/Function_Reference/wp_localize_script
- 0
- 2016-10-11
- Steve
-
Vedi anche quiper unesempio di ajaxurle localizzarlo.https://www.creare.co.uk/blog/wp/wp-localize-script-wordpressAlso see here for an example of the ajaxurl and localizing that. https://www.creare.co.uk/blog/wp/wp-localize-script-wordpress
- 0
- 2016-10-11
- Steve
-
OK,sto lottandoperfarlofunzionare.Homodificato lamia query originale soprapermostrare ciò che hoprovatoin base allatua rispostaOK, Im struggling to get this working. I have edited my original query above to show what I have tried based on your answer
- 0
- 2016-10-11
- LeeTee
-
Invece di "restituisci $file",esegui "echo $file".Instead of "return $files", do "echo $files".
- 0
- 2016-10-11
- Steve
-
OPPURE,fai wp_send_json ($files);https://codex.wordpress.org/Function_Reference/wp_send_jsonOR, do wp_send_json($files); https://codex.wordpress.org/Function_Reference/wp_send_json
- 0
- 2016-10-11
- Steve
-
Ok sono cambiatoin wp_send_json ($files);epurtroppo ottieni ancora 0Ok I have changed to wp_send_json($files); and still get 0 unfortunately
- 0
- 2016-10-11
- LeeTee
-
Prova: `functionfolder_contents () { echo 'ciao'; morire(); } " Vedi laparola ciao uscirein console.log?Try: `function folder_contents() { echo 'hello'; die(); }` Do you see the word hello come out in the console.log?
- 0
- 2016-10-11
- Steve
-
L'hoprovatoe ho ottenuto 0,ho rimossotutto da quellafunzionee ottengo ancora 0. Se vado direttamente alfile con http://myweb.co.uk/wp-admin/admin-ajax.php?action=folder_contents Iottieni ancora 0.Ive tried that and get 0, tred removing everything from that function and still get 0. If I go directly to the file with http://myweb.co.uk/wp-admin/admin-ajax.php?action=folder_contents I still get 0.
- 0
- 2016-10-11
- LeeTee
-
grazieperil vostro aiuto aproposito,èmolto apprezzato :)thansk for your help by the way, it is much appreciated :)
- 0
- 2016-10-11
- LeeTee
-
Cerchiamo di [continuare questa discussionein chat] (http://chat.stackexchange.com/rooms/46636/discussion-between-steve-north-and-leetee).Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/46636/discussion-between-steve-north-and-leetee).
- 0
- 2016-10-11
- Steve
-
- 2016-10-11
Neltuo codice vedo che localizzi lo script "ajax-js",ma lo script stesso è accodato?Ho dovuto usaregli script wp_localizepoche settimanefae ho dovuto accodareilmio script JS all'interno dellafunzione che chiamava script wp_localize.Adesempio:
add_action( 'wp_enqueue_scripts', 'google_js_script' ); function google_js_script() { // Enqueue the js script wp_enqueue_script('google-js', GOOGLE_PLG_URL . 'assets/js/google-places.js', array('jquery'), true); // Localize the enqueued JS script wp_localize_script( 'google-js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'place' => '' ) ); }
Spero che questo aiuti a risolvereilproblema.
In your code I see you localize the 'ajax-js' script, but is the script itself enqueued? I had to use wp_localize scripts a few weeks ago and I had to enqueue my JS script inside the function that called wp_localize script. For example:
add_action( 'wp_enqueue_scripts', 'google_js_script' ); function google_js_script() { // Enqueue the js script wp_enqueue_script('google-js', GOOGLE_PLG_URL . 'assets/js/google-places.js', array('jquery'), true); // Localize the enqueued JS script wp_localize_script( 'google-js', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'place' => '' ) ); }
Hope this helps to debug the issue.
-
Il JSnon viene utilizzato sul sito stesso.Sta usando JS all'interno di un'applicazione desktop.The JS is not used on the site itself. He is using the JS inside a desktop application.
- 0
- 2016-10-12
- Steve
-
- 2016-10-11
Il WP Codex afferma chepuoi utilizzare la variabileglobale
ajaxurl
senzaprima dichiararlo.In altreparole,commenta questa riga:var ajaxurl = ajax_params.ajax_url;
...e vedi sefunziona.
The WP Codex states that you can use the global variable
ajaxurl
without declaring it first. In other words, comment out this line:var ajaxurl = ajax_params.ajax_url;
...and see if that works.
Ho un'app desktope utilizzo $ .getJSON per ottenere dati dalmio sito wordpress utilizzandoilplug-in WP REST API . Funzionabene e quindipresumo dipoter ancheeffettuare una chiamata Ajax a unafunzionein functions.php? Comefaccio afare questo? Nessuno degliesempi che ho vistofornisce ajaxURL. Comefaccio a sapere di cosa sitratta?
Hoil seguente codice:
Functions.php (nel sito wordpress sul server)
app.js (eseguito utilizzandoilnodo webkit localmente dallamacchina)
------------------- MODIFICA ----------------------
Dopo la risposta di seguito hoprovatoil codice seguentein base al consiglioma ottengo
Functions.php
<"App.js"
Se rimuovo lafunzione add_ajax_script () e utilizzo semplicemente quanto segue,restituisce 0.
Perfavore aiutatemi ...