I need to apply an add_filter from my functions.php but only if the user has opened media-upload.php from my custom function. Is it possible to send a variable when opening media-upload.php that I could then test for existence of before executing add_filter?

Example: Here is my code in which I’m opening the media uploader from my custom icon…

//Custom upload launcher for custom attachments
function wpe_customImages($initcontext)
{
    global $post;
    ?>
<script type="text/javascript">
jQuery(document).ready(function() {
    var fileInput="";

    jQuery('#customAttachments').click(function() {
        fileInput = jQuery(this).prev('input');
        formfield = jQuery('#upload_image').attr('name');
        post_id = jQuery('#post_ID').val();
        tb_show('', 'media-upload.php?post_id='+post_id+'&amp;type=image&amp;TB_iframe=true');
        return false;
    });

});
</script>
    <?php
    return $initcontext. 
    '<input type="hidden" id="post_ID" value="'. $post->ID .'" />
    Product Images:<a id="customAttachments" href="https://wordpress.stackexchange.com/questions/4302/javascript:;" 
    class="mceButton mceButtonEnabled" onmousedown="return false;" 
    onclick="return false;">
    <img src="'.get_bloginfo('template_directory') .'/img/upload-icon.gif"" /></a>';
}
add_filter('media_buttons_context', 'wpe_customImages');

Inside the above code, would it be possible to set a global variable or some marker that I can test for in order to execute my add_filter code?

 if(isset($myMarker)){
  add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);
  }

2 Answers
2

From the looks of your code, clicking on the #customAttachments field is firing a jQuery event that calls a tb_show() method to load the media-upload.php file with certain GET parameters already set (post_id, type, TB_iframe). What you could do is add another GET parameter and check if that’s set in order to execute your add_filter() code.

So:

tb_show('', 'media-upload.php?post_id='+post_id+'&amp;type=image&amp;TB_iframe=true');

Becomes:

tb_show('', 'media-upload.php?post_id='+post_id+'&amp;type=image&amp;TB_iframe=true&amp;myMarker=true');

Then you can use:

if(isset($_GET['myMarker'])){
    add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);
}

Leave a Reply

Your email address will not be published. Required fields are marked *