|
View:
New views
17 Messages
—
Rating Filter:
Alert me
|
|
|
ajax to call function in themeI’m probably going about this the wrong way…
How would I use AJAX to call a function in my theme’s function file? I’ve got a page template that loads a google map, and what I’d like to do is use a function in my template’s functions.php file to return stuff based on what’s near the original location. So I’ve got a user-entered zip code, I’d like to look into my database for vendors near that zip code, and return them so I can use javascript to cycle through and place a marker on the map. The way I’m trying to do it is simply passing the url to the AJAX “open” method; this doesn’t work, as $wpdb var is not recognized at this point. Thoughts? -Sharon Chambers _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: ajax to call function in themeOn Thu, Oct 29, 2009 at 6:51 PM, Sharon Chambers <sharon@...>wrote:
> I’m probably going about this the wrong way… > How would I use AJAX to call a function in my theme’s function file? > The same way you would do it in a plugin: http://codex.wordpress.org/AJAX_in_Plugins -- http://scribu.net _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
|
|
|
|
|
|
Re: ajax to call function in themeOn Thu, Oct 29, 2009 at 9:39 PM, Sharon Chambers <sharon@...>wrote:
> Ok, I followed the tutorial, and am still unable to access $wpdb... > Unfortunately, the doc leaves that to the developer, which is the one thing > I can't figure out. > > Surely there's a way? > Just put this in functions.php: add_action('wp_ajax_myfunction', 'myfunction'); function myfunction() { global $wpdb; // do stuff here } -- http://scribu.net _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
|
|
|
Re: ajax to call function in themeLet me guess: You put this server side code into some separate PHP
file somewhere, and now are calling that PHP file directly from your AJAX call, yes? I suggest you re-read that tutorial, because that's exactly what you *don't* do, and the tutorial explains why. Here's a short example. All this will be in one file. function my_action_javascript( ) { var mysack = new sack( "<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php" ); mysack.execute = 1; mysack.method = 'POST'; mysack.setVar( "action", "my_special_action" ); mysack.setVar( "whatever", '1234' ); mysack.onError = function() { alert('error' )}; mysack.runAJAX(); return true; } add_action('wp_ajax_nopriv_my_special_action', 'my_action_callback'); function my_action_callback() { $whatever = $_POST['whatever']; // $whatever here will be 1234.. See? } Take special note of the javascript code which has an "action" variable. Also take note that it is calling the admin-ajax.php file, not some other file that you created. Then note that my_special_action is part of the first parameter in the add_action call. These are all significant. -Otto Sent from Memphis, TN, United States On Thu, Oct 29, 2009 at 2:47 PM, Sharon Chambers <sharon@...> wrote: > Realized that was a bit lacking... > Here's my simple test code following the AJAX tutorial in the codex (http://codex.wordpress.org/AJAX_in_Plugins): > > <?php > $vote = $_POST['vote']; > $results_id = $_POST['results_div_id']; > > global $wpdb; > $results = 'db prefix: '. $wpdb->prefix; > > die( "document.getElementById('$results_id').innerHTML = '$results'" ); > ?> > > The above produces empty string for prefix: db prefix: > > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: ajax to call function in themeOn Thu, Oct 29, 2009 at 3:02 PM, Otto <otto@...> wrote:
> Let me guess: You put this server side code into some separate PHP > file somewhere, and now are calling that PHP file directly from your > AJAX call, yes? > > I suggest you re-read that tutorial, because that's exactly what you > *don't* do, and the tutorial explains why. Unfortunately, the tutorial *does* suggest that very thing for the modified example: > We need to send our request to a plugin PHP file. This could be the main plugin PHP file, or a separate PHP file. It's probably a little cleaner to do it in a separate file, which we'll assume is called "myplugin_ajax.php", located in the standard wp-content/plugins directory of Wordpress. _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: ajax to call function in themeThat should probably be clarified then. If you're going to send AJAX
requests to separate files, then those separate files have absolutely no visibility to any part of WordPress at all. The normal case should be the action method, because you probably want to access WordPress or the database, in some manner. -Otto Sent from Memphis, TN, United States On Thu, Oct 29, 2009 at 3:05 PM, Austin Matzko <if.website@...> wrote: > On Thu, Oct 29, 2009 at 3:02 PM, Otto <otto@...> wrote: >> Let me guess: You put this server side code into some separate PHP >> file somewhere, and now are calling that PHP file directly from your >> AJAX call, yes? >> >> I suggest you re-read that tutorial, because that's exactly what you >> *don't* do, and the tutorial explains why. > > Unfortunately, the tutorial *does* suggest that very thing for the > modified example: > >> We need to send our request to a plugin PHP file. This could be the main plugin PHP file, or a separate PHP file. It's probably a little cleaner to do it in a separate file, which we'll assume is called "myplugin_ajax.php", located in the standard wp-content/plugins directory of Wordpress. > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: ajax to call function in themeOn Thu, Oct 29, 2009 at 3:07 PM, Otto <otto@...> wrote:
> That should probably be clarified then. If you're going to send AJAX > requests to separate files, then those separate files have absolutely > no visibility to any part of WordPress at all. > > The normal case should be the action method, because you probably want > to access WordPress or the database, in some manner. I agree. It's a bad example, and there's not even any mention of using wp-load.php, which is meant for people who insist on doing it that way anyways. _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
|
|
|
Re: ajax to call function in themeFinally updated the Codex example, using jQuery.
-- http://scribu.net _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: ajax to call function in themeI've also updated this in the Codex example that uses Sack. Took me a while to figure this so maybe it will help others now.
|
|
|
Re: ajax to call function in themeThere's still some inconsistency. Tutorial says: "Note: In WP 2.8, if
a user is not logged in ('wp_ajax_nopriv_my_action'), then GET requests are ignored (and return -1). This bug was fixed in WP 2.9." It probably means that for every wordpress with version less then 2.9 wp_ajax_nopriv_my_action hook doesn't work, right for not logged in users? And then it goes again - next section: "Ajax on the Viewer-Facing Side ( WordPress 2.6 , 2.7, 2.8 )" and: "add_action('wp_ajax_nopriv_my_special_action', 'my_action_callback');". At the same time Codex says that: "We need to send our request to a plugin PHP file. This should be the main plugin PHP file. In case you want it to be a separate PHP file, bare in mind that you won't be able to access the Wordpress global variables, e.g. $wpdb, since separate files have absolutely no visibility to any part of WordPress at all." I think having all that together, no new comer will be able to figure out howto write an ajax call for not logged-in users for WP version less then 2.9 (current stable release that is). On Tue, Nov 17, 2009 at 8:40 PM, nunomorgadinho <nuno.morgadinho@...> wrote: > > I've also updated this in the Codex example that uses Sack. Took me a while > to figure this so maybe it will help others now. > > > scribu wrote: >> >> Finally updated the Codex example, using jQuery. >> >> -- >> http://scribu.net >> _______________________________________________ >> wp-hackers mailing list >> wp-hackers@... >> http://lists.automattic.com/mailman/listinfo/wp-hackers >> >> > > -- > View this message in context: http://old.nabble.com/ajax-to-call-function-in-theme-tp26116790p26393151.html > Sent from the Wordpress Hackers mailing list archive at Nabble.com. > > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers > -- Davit Barbakadze _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: ajax to call function in themeOn Fri, Dec 11, 2009 at 10:46 AM, Davit Barbakadze <jayarjo@...> wrote:
> There's still some inconsistency. [snip] > I think having all that together, no new comer will be able to figure > out howto write an ajax call for not logged-in users for WP version > less then 2.9 (current stable release that is). As a "wiki," the Codex can be edited by anybody, so that means both that you can expect inconsistency and that you should feel free to correct it when you find it. _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: ajax to call function in themeI had a similar discussion on trac
(http://core.trac.wordpress.org/ticket/11062). I think before we work on updating the codex, we should come up with a recommended solution for handling ajax requests from the front end. The problem with always using admin-ajax is that some sites have the admin in a different domain and or have the admin htaccess protected. Not all users know how to make exceptions in the htaccess file to allow the ajax to work. Davit Barbakadze wrote: > There's still some inconsistency. Tutorial says: "Note: In WP 2.8, if > a user is not logged in ('wp_ajax_nopriv_my_action'), then GET > requests are ignored (and return -1). This bug was fixed in WP 2.9." > > It probably means that for every wordpress with version less then 2.9 > wp_ajax_nopriv_my_action hook doesn't work, right for not logged in > users? And then it goes again - next section: "Ajax on the > Viewer-Facing Side ( WordPress 2.6 , 2.7, 2.8 )" and: > "add_action('wp_ajax_nopriv_my_special_action', > 'my_action_callback');". At the same time Codex says that: "We need to > send our request to a plugin PHP file. This should be the main plugin > PHP file. In case you want it to be a separate PHP file, bare in mind > that you won't be able to access the Wordpress global variables, e.g. > $wpdb, since separate files have absolutely no visibility to any part > of WordPress at all." > > I think having all that together, no new comer will be able to figure > out howto write an ajax call for not logged-in users for WP version > less then 2.9 (current stable release that is). > > On Tue, Nov 17, 2009 at 8:40 PM, nunomorgadinho > <nuno.morgadinho@...> wrote: > >> I've also updated this in the Codex example that uses Sack. Took me a while >> to figure this so maybe it will help others now. >> >> >> scribu wrote: >> >>> Finally updated the Codex example, using jQuery. >>> >>> -- >>> http://scribu.net >>> _______________________________________________ >>> wp-hackers mailing list >>> wp-hackers@... >>> http://lists.automattic.com/mailman/listinfo/wp-hackers >>> >>> >>> >> -- >> View this message in context: http://old.nabble.com/ajax-to-call-function-in-theme-tp26116790p26393151.html >> Sent from the Wordpress Hackers mailing list archive at Nabble.com. >> >> _______________________________________________ >> wp-hackers mailing list >> wp-hackers@... >> http://lists.automattic.com/mailman/listinfo/wp-hackers >> >> > > > > _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: ajax to call function in themeOn Fri, Dec 11, 2009 at 10:46 AM, Davit Barbakadze <jayarjo@...> wrote:
> There's still some inconsistency. Tutorial says: "Note: In WP 2.8, if > a user is not logged in ('wp_ajax_nopriv_my_action'), then GET > requests are ignored (and return -1). This bug was fixed in WP 2.9." > > It probably means that for every wordpress with version less then 2.9 > wp_ajax_nopriv_my_action hook doesn't work, right for not logged in > users? No. It means that you can't use GET. So you have to use POST. In 2.9, both GET and POST work. > And then it goes again - next section: "Ajax on the > Viewer-Facing Side ( WordPress 2.6 , 2.7, 2.8 )" and: > "add_action('wp_ajax_nopriv_my_special_action', > 'my_action_callback');". At the same time Codex says that: "We need to > send our request to a plugin PHP file. This should be the main plugin > PHP file. In case you want it to be a separate PHP file, bare in mind > that you won't be able to access the Wordpress global variables, e.g. > $wpdb, since separate files have absolutely no visibility to any part > of WordPress at all." That is for WP versions prior to the nopriv actions being added. Which happened back in March: http://core.trac.wordpress.org/changeset/10720/trunk/wp-admin/admin-ajax.php > I think having all that together, no new comer will be able to figure > out howto write an ajax call for not logged-in users for WP version > less then 2.9 (current stable release that is). The nopriv actions are there in in 2.8 and up. So probably the codex needs editing. On Fri, Dec 11, 2009 at 10:57 AM, Michael Pretty <mpretty@...> wrote: > I had a similar discussion on trac > (http://core.trac.wordpress.org/ticket/11062). I think before we work on > updating the codex, we should come up with a recommended solution for > handling ajax requests from the front end. The problem with always using > admin-ajax is that some sites have the admin in a different domain and or > have the admin htaccess protected. Not all users know how to make > exceptions in the htaccess file to allow the ajax to work. > Davit Barbakadze wrote: Using an SSL admin won't make any difference. Using an admin on a different domain might be a problem, but that's a pretty custom setup to begin with. I don't think we need explicit support for that type of configuration. That said, you can always use the init or the template_redirect action to do AJAX requests to the main blog URL instead of going through the explicit admin-ajax code. -Otto Sent from Memphis, TN, United States _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
| Free embeddable forum powered by Nabble | Forum Help |