SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot XandroZ's Avatar
    Join Date
    May 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wordpress and wp_ajax

    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

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This quick URL I found covers it, there's not much to do:

    http://geekpreneur.blogspot.com/2009...wordpress.html

    1) Add a function to a plugin that you want to be available through AJAX
    2) Register it with add_action("wp_ajax_make_up_this_part", "your_function_name");
    3) Call that function from JavaScript by POSTing to /wp-admin/admin-ajax.php with "action" parameter equal to the name you defined in step 2

  3. #3
    SitePoint Zealot XandroZ's Avatar
    Join Date
    May 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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).

    That's everything...

  5. #5
    SitePoint Zealot XandroZ's Avatar
    Join Date
    May 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code:
    <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 Code:
    <?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;

    }

    ?>

  6. #6
    SitePoint Zealot XandroZ's Avatar
    Join Date
    May 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After some tests I observed that the solution is working if everything is on the same file.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •