I want to use $WPDB class outside Wordpress context in an external Ajax handling script.
For this in general is needed to load the core Worpdress files.
After loading those core files I can make calls to the MySQL tables using the $wpdb class, but loading the core Wordpress files is a lot of overhead just to run a couple of queries. I was looking for a better solution and I find ‘wp_ajax’
and ‘wp_ajax_nopriv’ hooks, but on Wordpress site and on Google I couldn’t find any good documentation or tutorial.
I need to transfer, using ajax, back and forth data as Json.
Could anyone provide me with more information or a good example about this?
Thx
Thx, but I know that tutorial.I searched 3 days on google.My situation is a little different, plus the tutorial is not complete, the author wrote “continue… next post I will show you the full plugin code” , but doesn’t exist a next post.For the moment I’m not creating a plugin.
But maybe, you can help me, if i didn’t understand correctly the tutorial.
In wordpress functions.php file I have a function which control a form, which can have tens to hundreds of fields.This function is used to modify different fields in all the site(pages and posts). It is a mass editing function.On submit on the form, with jquery $post I send Json data to a script, that send me back json data, as array.
This script need to use $wpdb.In his case the author use only 1 file, by redirecting the ajax call thru wordpress admin-ajax.php.In my case are 2 files implied, and for the moment I didn’t have an working solution
You don’t need anything else but what’s in that tutorial.
Your “mass editing function” is the function you register with add_action (step 2). Then your AJAX call posts the form to admin-ajax.php with an “action” equal to the name you used when registering your “mass editing function” (step 3).
It is not working; The values are send correct but for the response I get (Firebug) zero.The code is working loading wordpress core.
Front-end in functions.php
<script>
$j=jQuery.noConflict();
$j('document').ready(function(){
$j('#custom_select_seo').change(function(){
$j.post('admin-ajax.php', {action:"select", option: $j('#custom_select_seo').val(),custom_select: "custom_select"},
function(data){
for (var i in data) {
$j("#edit_"+data[i][0]).val(data[i][1]);
i++;
$
}
}, 'json');
})
});
</script>
<?php add_action("wp_ajax_select", "my_ajax_update"); ?>
The php script is located at the same level in theme file:
<?php
function my_ajax_update() {
if ($_POST['custom_select']=='custom_select')
$ld=loadInfo ($_POST['option']);
}
function loadInfo ($option){
global $wpdb;
switch ($option) {
case 'title':
$res=$wpdb->get_results("SELECT {$wpdb->posts}.ID AS ID,{$wpdb->posts}.post_title ,
{$wpdb->postmeta}.meta_value AS meta_value
FROM {$wpdb->posts}
JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID={$wpdb->postmeta}.post_id
AND {$wpdb->posts}.post_type='post'AND {$wpdb->postmeta}.meta_key='title'");
break;
case 'description':
$res=$wpdb->get_results("SELECT {$wpdb->posts}.ID AS ID,{$wpdb->posts}.post_title ,
{$wpdb->postmeta}.meta_value AS meta_value
FROM {$wpdb->posts}
JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID={$wpdb->postmeta}.post_id
AND {$wpdb->posts}.post_type='post'AND {$wpdb->postmeta}.meta_key='description'");
break;
case 'keywords':
$res=$wpdb->get_results("SELECT {$wpdb->posts}.ID AS ID,{$wpdb->posts}.post_title ,
{$wpdb->postmeta}.meta_value AS meta_value
FROM {$wpdb->posts}
JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID={$wpdb->postmeta}.post_id
AND {$wpdb->posts}.post_type='post'AND {$wpdb->postmeta}.meta_key='keywords'");
break;
}
$i=1;
foreach ($res as $seo) {
$list[$i]=array($seo->ID,$seo->meta_value);
$i++;
// $list['edit_'.$seo->ID][ID]=$seo->ID;
//$list['edit_'.$seo->ID][$_POST['option']]=$seo->meta_value;
}
$str=json_encode($list);
echo $str;
}
?>