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+'&type=image&TB_iframe=true');
return false;
});
});
</script>
<?php
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?
if(isset($myMarker)){
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 atb_show()
method to load themedia-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 youradd_filter()
code.So:
Becomes:
Then you can use:
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 whenwp-includes/vars.php
get’s included, which is done bywp-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:
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: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 thecustomImages()
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.