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;

    return $initcontext. 
    '<input type="hidden" id="post_ID" value="'. $post->ID .'" />
    Product Images:<a id="customAttachments" href="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?

  add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 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.


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


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

Then you can use:

    add_filter("attachment_fields_to_save", "rt_image_attachment_fields_to_save", null , 2);
  • Brilliant! I figured it was something simple :) Thanks! – Scott B Nov 19 '10 at 18:15

Even this question has already an accepted answer, I will try to give a more broad answer to the problem.

If you want to get the name of the page the user has requested, there is a global variable that stores it: $pagenow. This works in both, the admin-area and the website (frontpage). This global variable is always set (it's initialized when wp-includes/vars.php get's included, which is done by wp-settings.php, so directly when your wp-config.php file is loaded (at the end of to be precise)).

So if the file media-upload.php is requested, $pagenow would be set to "media-upload.php".

So you could already simply check for it:

if ('media-upload.php' === $pagenow ) { ... do your stuff ... }

To improve this further on, think about only registering your filter when you really need it. This helps to keep the actual filter function "on topic". And by that reduces the complexity which is always good. You have not posted the code of your wpe_customImages() function, but consider the following. I wrote this as a class, but it works with just global functions as well:

class ConditionalFilterPlugin {
    public function __construct() {
        add_filter('init', array($this, 'init'));
    public function init() {
        if ('media-upload.php' === $GLOBALS['pagenow'])
            add_filter('media_buttons_context', array($this, 'customImages'))
    public function customImages() {
        // your hook function's code that does not need a conditional any longer.
$ConditionalFilterPlugin = new ConditionalFilterPlugin();

This stub registers a second function on the init hook. The init hook is called when wordpress is "ready to use", which means you can use most of what the wordpress API offers.

Inside the init() function, the pre-requisites to register the customImages() function are checked before actually registering it. This saves you not only from removing the decision from within your hook callback, but also enables you to re-use the callback function in other places.

  • I only want to call the add_filter when media-upload.php is triggered from my custom upload icon. $pagenow will always be the same. To solve it, I had to add a custom querystring parameter and wrap add_filter with if(isset($_GET['my-custom-param'])) – Scott B Nov 19 '10 at 21:05
  • @Scott B: Now I see. I should have read your question more carefully as you already have a conditional hook in there ... :) – hakre Nov 19 '10 at 21:42

Your Answer

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

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