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 have a custom media upload button (in the standard post edit page, alongside the standard media upload button), and need to change the upload directory dynamically if this button is clicked - using add_filter('wp_handle_upload_prefilter', 'my_function').

This post & answer: Conditional add_filter? seem to give the solution, but for some reason don't work for me - the additional parameter 'hexProtected' is not set & so add_filter is not called.

My code is below... am I doing something stupid or does the solution posted not work?!

<?php
/* Custom Upload Directory for protected posts */                                    

function hex_protected_media_button($context) {
    global $post;
    $media_button_image = 'http://www.example.com/wp-admin/images/media-button.png?ver=20111005';
    $media_button = ' %s' . '<a href="media-upload.php?post_id='.$post->ID.'&amp;TB_iframe=1&amp;hexProtected=1" class="thickbox"><img src="'.$media_button_image.'" /></a>';
    return sprintf($context, $media_button);
}

add_filter('media_buttons_context', 'hex_protected_media_button');                                                           
add_filter('wp_handle_upload_prefilter', 'hex_pre_upload');
add_filter('wp_handle_upload', 'hex_post_upload');

function hex_pre_upload($file){  
    if (isset($_GET['hexProtected'])) {
       add_filter('upload_dir', 'hex_custom_upload_dir');
    }
    return $file;
}

function hex_custom_upload_dir($path){      
    if(!empty($path['error'])) { return $path; } //error; do nothing.   
    $path['path'] = str_replace($path['subdir'], '/protected'.$path['subdir'], $path['path']);
    $path['url'] = str_replace($path['subdir'], '/protected'.$path['subdir'], $path['url']);        
    $path['subdir']  = '/protected';    
    return $path;
}

?>
share|improve this question
1  
What exact is "don't work for me"? Error? Notice? Warning? Debug output? –  kaiser Oct 31 '12 at 19:08
    
@kaiser - sorry! Have updated the post - no error as such, the parameter just doesn't seem to be set. –  Sarah Nov 1 '12 at 10:10
    
Please add some debug information to your question. Currently we can't help you, as the needed information isn't available. –  kaiser Nov 1 '12 at 15:03
1  
Notice: Undefined variable: cache_needs_updating in ../public_html/wp-content/plugins/s2member/includes/classes/cache.inc.php on line 97 and Notice: Undefined index: page in ../public_html/wp-content/plugins/s2member/includes/classes/admin-notices.inc.ph‌​p on line 136 –  Sarah Nov 1 '12 at 15:15
1  
@kaiser - thanks. I have created a new test site, with a vanilla WP install & TwentyEleven theme. No plugins installed, wp_debug turned on. I then added my code to functions.php to get my additional media upload button. As previously, no errors/warnings/notices produced when I upload with this button. But it still does not change the upload directory. –  Sarah Nov 2 '12 at 9:11
show 10 more comments

1 Answer 1

I know it's been a while, but I wanted to offer a solution, if only for those who come across this question via Google.

The issue is with the order of the parameters in the query string when you build the URL for the media uploader. My experience is that any parameters that come after 'TB_iframe=1' are ignored, so you want to put your custom values before this. I haven't seen this documented (and haven't explored core to see why the parameters are handled this way), but can't think of any good reason for it--I probably spent an hour tearing my hair out before I discovered the issue.

I also haven't tried this in 3.5 yet. With all the changes to the media uploader it's possible that this has been addressed.

share|improve this answer
    
add comment

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.