I've done some research but I can't seem to find the correct answer so I'm hoping someone can give me a hand. I'm attempting to use the wp_ajax hook in the admin section. The functionality should be that when a user clicks a button, I will be adding the value to a custom options array.

I'm using a class for this

public function __construct() {
    // Add small js to footer
    add_action( 'admin_footer', array( $this, 'render_vendor_type_js' ) );

    // Ajax handler
    add_action( 'wp_ajax_ajax_add_vendor_type', array( $this, 'ajax_add_vendor_type_cb' ) );

My js added in the footer is as follows:

public function render_vendor_type_js() { ?>
    <script type="text/javascript" >
    jQuery(document).ready(function($) {
        $("#add-vendor-type-button").click(function() {
            // Retrieve the value the user entered into the add vendor textbox
            var vendor_type_text = $("#add-vendor-type").val();

            // Add data for Ajax call
            var data = {
                'action': 'ajax_add_vendor_type',
                'new_vendor_type': vendor_type_text

            // post dat to built in wp admin-ajax.php
            $.post(ajaxurl, data, function(response) {
                alert("Got this from the server: " + response);

The call back is not quite developed, but it's just enough for testing purposes.

public function ajax_add_vendor_type_cb() {
    $vendor_type = $_POST['new_vendor_type'];
    echo $vendor_type;

And here is the text box I provide the user to enter data into.

public function add_vendor_type_cb( $args ) {

        <input type="text" id="add-vendor-type" name="oewpc-vendor-type" />
        <input type="button" id="add-vendor-type-button" class="button-small secondary-button" value=" Add Vendor " />

    OEWPC_Admin_Utilities::print_description( $args[0] );

So, when the user enters data into the textbox I'm alerted with a message saying "Got this from server: 0". Then it appears that the callback function I've defined in the ajax action never fires as the alert box is the only indication I get that anything happened.

Any help would be great on this!

  • Maybe: add_action( 'wp_ajax_ajax_add_vendor_type', array( $this, 'ajax_add_vendor_type_cb' ) ); – shanebp Jun 1 '15 at 21:45
  • Apologies...I do have the code pointing to it's own instance with $this...that was a bit of old code I accidentally put in. I've updated accordingly....still the same issue. – user3306212 Jun 1 '15 at 23:17
  • How are you enqueing the js file? – sakibmoon Jun 2 '15 at 1:38
  • I'm not enqueing the file, it is inline in the footer via the render_vendor_type_js function. I did have it enqueued at one point but the same issue existed. – user3306212 Jun 2 '15 at 2:27
  • where do you create an instance of your class? – Milo Jun 2 '15 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.