• Hi there,

    Has anyone here had any experience using the ‘add_attachment’ hook? Basically, here’s what I’m trying to achieve:

    In the admin area I have a custom post type that includes a new meta box called ‘Attachments’. This box displays a list all the files that are currently attached to this post. I have all this working so far but the problem comes when someone adds a new attachment through the ‘Add Media’ facility; since the page doesn’t refresh when someone uploads a new file, there is no way of my ‘Attachments’ meta box knowing that a new file has been attached.

    To get around this I was hoping to use some jQuery inside the ‘add_attachment’ hook in my functions.php to append the html for the new file to my meta box.

    Here’s my code so far:

    add_action('add_attachment', 'add_message_attachment');
    function add_message_attachment($attachment_id) {
    	$attach = get_post($attachment_id);
    	$str	= '<li>'.$attach->post_title.'</li>';
    ?>
    <script>
    jQuery(document).ready(function() {
    	jQuery("#messageattach ul").append('<?php echo $str; ?>');
    });
    </script>
    <?php
    }

    This doesn’t seem to be doing anything yet except mysteriously outputting the ID of the attachment into the upload progress bar of the File Upload dialog box.

    Any ideas?

  • The topic ‘Using add_attachment hook with jquery’ is closed to new replies.