WordPress.org

Ready to get started?Download WordPress

Forums

Problem with plugin because i am using ajax (3 posts)

  1. jonathan.derouchie
    Member
    Posted 5 years ago #

    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.

  2. jonathan.derouchie
    Member
    Posted 5 years ago #

    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;
    }

  3. seborgarsen
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic