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'm trying to implement AJAX into my post edit form in order to hook a custom wp function into a link click. So far I've been able to register my custom js file into my admin header. However when testing my link click nothing is happening unless I do an alert() in my jQuery.

Here is my code for the href link I want to hook into my custom function:

<a href="#" id="delete-' . $field['id'] . '" class="delete_pdf" title="Delete this PDF." >Delete</a>

Here's my code in my functions.php for registering, enqueue, and localizing scripts, as well as my custom function (still work in progress):

//Add AJAX functionality to post.php to delete files
add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');
add_action('wp_ajax_delete_meta', 'delete_pdf_and_metadata');

//Add my custom JS to the header of admin
function my_admin_enqueue_scripts($hook) {
    global $current_screen;

    if ( 'post.php' != $hook )
        return;
    wp_register_script('my-scripts', get_template_directory_uri() . '/js/custom/my-scripts.js' );
    wp_enqueue_script('my-scripts');
    wp_localize_script('my-scripts', 'wp_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
}

function delete_pdf_and_metadata() {

    global $post;

    $the_id = intval($_POST['the_id'] );
    //echo $the_id . '<br />';  //debugging
    //echo $key . '<br />';  //debugging
    wp_die($the_id);   //debuggin

    //delete_post_meta( $post->ID, $key );  //TODO
}

Here's my jQuery in my my-scripts.js file I've successfully registered:

jQuery(document).ready(function($) {
    $('.delete_pdf').each(function(i,e) { //grab the class delete-pdf
        var id = $(this).attr('id').replace(/delete-/, '');
        $(this).click(function(){
            //alert('Clicked! '+ $(this).attr('id'));  //THIS WORKS WHEN CLICKED
            $.post(wp_ajax.ajaxurl, { action: 'delete_meta', the_id: id }, function(data){
                });
        });
    });

    function replace_pdf_with_file_upload(){
        //TODO write to replace PDF link with File Upload

    }
});

In my jQuery, if I do an alert() when clicking the link it works fine. However when I try to test my custom function delete_pdf_and_metadata() by simply doing a wp_die(), I get absolutely nothing. Not even exceptions in firebug where I'm debugging javascript.

I think it has to do with the way I localized the scripts, but I'm not sure. this is my first dive into using AJAX with WordPress.

Am I missing something?

share|improve this question
    
On the admin side, ajaxurl is already available as a global javascript variable, so you don't actually need that localizing part. What is the response of the ajax call if you enable debugging? (DEBUG=TRUE in config file) –  Parham Dec 5 '12 at 11:59
    
Thanks for the comment, when I comment out this line: wp_localize_script('my-scripts', 'wp_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ))); And click my link to call my ajax I get the error "wp_ajax is undefined" in my javascript error console. Also enabling debugging doesn't give me any info on the ajax call... –  drpcken Dec 5 '12 at 12:08
    
in your JS file replace wp_ajax.ajaxurl with ajaxurl. –  Parham Dec 5 '12 at 12:11
    
Did that, still nothing. Can't even get a simple alert() inside my function to fire. it's not firing the function and I'm not sure why. No errors in javascript console though... –  drpcken Dec 5 '12 at 12:20
    
Dunno if I'm onto something but... Since we're passing 'wp_ajax' into 'wp_localize_script' I tried doing this in my JS file: alert(wp_ajax.alert);. It alerts 'UNDEFINED'. Am I passing the correct values into my localize script function? –  drpcken Dec 5 '12 at 12:26

1 Answer 1

Based on your comments above, you are just passing a wrong id or you need to modify delete_pdf_and_metadata function. intval('aps_pdf1') is 0, and that's why your response is 0!

share|improve this answer
    
Ok I'll work on that! What should it return if it worked? THANK YOU ! –  drpcken Dec 5 '12 at 13:05

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.