WordPress.org

Support

Support » Plugins and Hacks » Problem with plugin because i am using ajax

Problem with plugin because i am using ajax

  • jonathan.derouchie
    Member

    @jonathanderouchie

    Hi I am working on my first plugin, and its going really well except for a small issue. i am trying to use $wpdb to modify the plugins data but because i am calling it with ajax, the class is not instantiated.

    after major google searches i figure i need to include wp-blog-header.php which makes sense however, i can’t load it and keep receiving this error:

    Warning: include(/wp/wp-blog-header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\wordpress\wp\wp-content\plugins\automatik\products_ajax.php on line 6

    is there some trick to including it? please trust me its not a path issue.

    since the ajax is simply adding/editing database content, i am considering whether it might be better just use a separate class for this functionality rather than load all the wordpress backbone for a couple of queries. any feedback or thoughts on this are also very much appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • jonathan.derouchie
    Member

    @jonathanderouchie

    so i found my solution, wp-blog-header is not needed.

    in my ajax i was calling the php page directly when i need to go through /wp-admin/admin-ajax.php. this is the most elegant solution because it will also take care of authorization.

    in jquery this looked like:


    function updateProduct(name, value) {
    jQuery.post(
    "/wp/wp-admin/admin-ajax.php", {
    action: "update_product",
    'cookie': encodeURIComponent(document.cookie),
    'name': value
    },function(msg) {
    jQuery('#debugger').append(msg+'
    ');
    });
    }

    then in my plugin top page i added an action


    add_action('wp_ajax_update_product', 'updateProduct' );

    and then the function to access the database as per the action:


    function updateProduct() {
    global $wpdb;
    $table_name = $wpdb->prefix . "ajax_test";
    $query = "INSERT INTO $table_name ($insert_key) VALUES ($insert_value)";
    }

    exit;
    }

    seborgarsen
    Member

    @seborgarsen

    Don’t you get name collision by having two function updateProduct?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem with plugin because i am using ajax’ is closed to new replies.