Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code in functions.php

<script type="text/javascript">
var post_id = "1055"; // hardcoded post id for testing purposes
var type = "some_type";
var data = {action: "get_variations", parent_id: post_id, item_type: type};
jQuery.post("/wp-admin/admin-ajax.php", data, function(response){
    alert(response);
});
</script>
<?php
function get_variations($parent_id, $item_type){

// etc..

}

add_action('wp_ajax_get_variations', 'get_variations', 10, 2);
?>

When ajax tries to call get_variations I always get:

Warning: Missing argument 2 for get_variations()

What am I doing wrong?

share|improve this question

2 Answers 2

Ajax-calls use the $_POST-variable to submit their arguments to the function. As $_POST['action'] is always used by Wordpress Ajax calls (contains the name of the action, obviously ;) ), PHP only complains over missing argument no. 2.

You can either use the solution provided by Bainternet. If you want to use your function in ajax as well as "pure" PHP context, you can do it this way:

    <?php
    function get_variations($parent_id = false, $item_type = false){

    if(isset($_POST['parent_id'])) {
        $parent_id = $_POST['parent_id'];
        $item_type = $_POST['item_type'];
    }

// etc..

}

This way, PHP will always assume the two arguments given, and you can also use this function without $_POST.

share|improve this answer

Your ajax callback has no arguments,

this should work:

function get_variations(){

    $parent_id = $_POST['parent_id'];
    $item_type = $_POST['item_type'];
// etc..

}

add_action('wp_ajax_get_variations', 'get_variations');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.