add_action ('wp_ajax_ [nom de l'action]', ma fonction) problème
-
-
@ user1567: Vous ajoutez lafonction aux hooks `wp_ajax_myaction`et` wp_ajax_admin_myaction`.Celane devrait-ilpas être `wp_ajax_myaction` (pour les utilisateurs connectés)et` wp_ajax_nopriv_myaction` (pour les utilisateurs anonymes)?@user1567: You add the function to the `wp_ajax_myaction` and `wp_ajax_admin_myaction` hooks. Should that not be `wp_ajax_myaction` (for logged in users) and `wp_ajax_nopriv_myaction` (for anonymous users)?
- 0
- 2010-12-01
- Jan Fabry
-
ouije l'ai réparé,merci.Aufait,j'ai un appel update_option dans le "ajax_function" qui semble échouer carilest appelé unefois quetoute la séquence de chargement wordpressestterminée?Existe-t-il un autremoyen de update_option?yes I fixed it, thanks. By the way, I have an update_option call inside the "ajax_function" that seem to fail because it is called after all wordpress loading sequence is complete? Is there another way to update_option?
- 0
- 2010-12-01
- user1567
-
2 réponses
- votes
-
- 2010-12-01
Dansmesprojets,je lefais comme ça
PHP
function foo() { echo 'bar'; } add_action('wp_ajax_foo', 'foo' ); // executed when logged in add_action('wp_ajax_nopriv_foo', 'foo' ); // executed when logged out
Javascript
data = { action: 'foo', avalue: 'some value', 'anothervalue': 'another value' }; jQuery.post(ajaxurl, data, function(response){ alert(response); });
In my projects I do it like that
PHP
function foo() { echo 'bar'; } add_action('wp_ajax_foo', 'foo' ); // executed when logged in add_action('wp_ajax_nopriv_foo', 'foo' ); // executed when logged out
Javascript
data = { action: 'foo', avalue: 'some value', 'anothervalue': 'another value' }; jQuery.post(ajaxurl, data, function(response){ alert(response); });
-
Merci d'avoir répondu.Je connais ceguide,je l'aiparcouru aussi.J'ai changé lafonctionpour qu'elle soitexactement comme la vôtremaispas de chance :-( Lafonctionn'esttoujourspas appelée.Thanks for the answer. I know this guide, I went over it as well. I changed the function to be exactly like yours but no luck :-( The function is still not called.
- 1
- 2010-12-01
- user1567
-
aufait,où définissez-vous la valeur de la variable ajaxurl?by the way, where do you set the value of ajaxurl variable?
- 0
- 2010-12-01
- user1567
-
Dansmonexemple,j'ai supposé que la variable était déjà définie.Dans WP Backend,la variableesttoujours définie.Dans lefrontend,je le définis simplement dans l'en-tête.
var ajaxurl= '/wp-admin/admin-ajax.php'; Jeparie qu'ilexiste unemanièreplusintelligente degérer cela. In my example I assumed that variable is already set. In WP Backend the variable is always set. In the frontend I just define it in the header. I bet there is a smarter way to handle it.- 1
- 2010-12-01
- Horttcore
-
Je vois.detoutefaçon,lafonctionne fonctionnetoujourspaspourmoi. Est-ilimportant que l'ajax soit destiné auback-end administrateur?ou lesmêmesfonctions doivent être utilisées?I see. anyway, the function still doesn't work for me. Does it matter that the ajax is for admin back end? or the same functions should be used?
- 0
- 2010-12-01
- user1567
-
Je l'aifinalement réparé.Leproblème était l'ordre de chargementet l'emplacement des appels add_action. J'ai une autrepetite question:tu as écrit data={action: 'foo',avalue: 'some value','anothervalue': 'another value'}; Quefaire sije souhaiteinclure untableau comme valeur de l'une des clés?I finally fixed it. The problem was the loading order and the location of the add_action calls. I have another small question: you wrote data = { action: 'foo', avalue: 'some value', 'anothervalue': 'another value' }; What if I want to include an array as the value of one of the keys?
- 0
- 2010-12-01
- user1567
-
C'est lié àjQuery comment vous voulez ajouter des donnéespour la demande ajax.http://api.jquery.com/jQuery.post/recherchez lesexemples,ilexiste différentesmanières d'inclure des données dans la requête.Thats related to jQuery how you want to add Data for the ajax request. http://api.jquery.com/jQuery.post/ look for the examples, there are different ways how to inlcude data in the request.
- 0
- 2010-12-02
- Horttcore
-
- 2016-07-03
Veuillez suivre le code:
add_action( 'wp_ajax_add_myfunc', 'prefix_ajax_add_myfunc' ); add_action( 'wp_ajax_nopriv_add_myfunc', 'prefix_ajax_add_myfunc' ); function prefix_ajax_add_myfunc() { // Handle request then generate response using WP_Ajax_Response }
et dans votre appel ajax,procédez comme suit:
jQuery.post( ajaxurl, { 'action': 'add_myfunc', 'data': 'foobarid' }, function(response){ alert('The server responded: ' + response); } );
dans l'appel ajax,vous appellerez votrefonction sans
prefix_ajax_
.Appelez seulementparil reste.Dans ce cas,c'estadd_myfunc
.Dans la réponse,ilenverradone
sitout vabien.Sinon,la réponse sera0
ou-1
.J'espère que cela aidera.Merci.
Please follow the code:
add_action( 'wp_ajax_add_myfunc', 'prefix_ajax_add_myfunc' ); add_action( 'wp_ajax_nopriv_add_myfunc', 'prefix_ajax_add_myfunc' ); function prefix_ajax_add_myfunc() { // Handle request then generate response using WP_Ajax_Response }
and in your ajax call do this:
jQuery.post( ajaxurl, { 'action': 'add_myfunc', 'data': 'foobarid' }, function(response){ alert('The server responded: ' + response); } );
in the ajax call you'll call your function without
prefix_ajax_
. Only call by it's remaining. In that case it'sadd_myfunc
. In the response it will senddone
if everything goes right. Else response will be0
or-1
.Hope it will help. Thank you.
J'essaye d'intégrer ajax dans wordpressen utilisant les directives du codex wp. Dans le PHP,j'ai ajouté:
Le code ajaxest:
et lafonction PHP qui devrait être appeléepar ajaxest:
L'appel ajaxest réussi (l '"alerte"fonctionne),cependant,lafonctionphp "ajax_function"n'estjamais appelée. Après avoireffectué un certain débogage,j'ai réalisé quemême si l'appel à l'action add_action ('wp_ajax_ définit unnouvel élément dans letableauglobal $ wp_filter,lorsque le do_action correspondant s'exécute dans admin-ajax.php,letableau $ wp_filterne contientplus cet élément./p>
Par conséquent,lafonction "ajax_function"estignorée. Uneidée depourquoi lafonctionn'estpas appelée?