我有一个自定义媒体上载按钮(在标准后期编辑页面中,与标准媒体上载按钮一起),如果单击此按钮,需要使用add\\u filter(“wp\\u handle\\u upload\\u prefilter”,“my\\u function”)动态更改上载目录。
此帖子(&P);答复:Conditional add_filter? 似乎给出了解决方案,但由于某些原因,对我不起作用-未设置附加参数“hexProtected”&;因此未调用add\\u筛选器。
我的代码如下。。。我是在做傻事还是发布的解决方案不起作用?!
<?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.\'&TB_iframe=1&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;
}
?>