我目前正在使用Wordpress插件,我喜欢为wordpress媒体上传器设置一个自定义上传目录,我在我的插件的一个子页面上使用。我已经搜索了整整一个星期,但我找不到我的错误或解决我的问题 - 所以我希望有人可以提供帮助。
我添加了一个子菜单页面,我调用了一个加载WP Media Uploader的方法:
add_submenu_page('w3p-listing', $pubAtt, $pubAtt, 'manage_options', 'w3p-attachments', array('w3pUploader', 'media_upload_init') );
这会调用类w3pUploader上的media_upload_init方法,如下所示:
class w3pUploader {
function __construct()
{
}
public static function media_upload_init() {
// jQuery
wp_enqueue_script('jquery');
// This will enqueue the Media Uploader script
wp_enqueue_media();
?>
<div>
<label for="image_url">Image</label>
<input type="text" name="image_url" id="image_url" class="regular-text">
<input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image">
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#upload-btn').click(function(e) {
e.preventDefault();
var image = wp.media({
title: 'Upload Image',
// mutiple: true if you want to upload multiple files at once
multiple: false
}).open()
.on('select', function(e){
// This will return the selected image from the Media Uploader, the result is an object
var uploaded_image = image.state().get('selection').first();
// We convert uploaded_image to a JSON object to make accessing it easier
// Output to the console uploaded_image
console.log(uploaded_image);
var image_url = uploaded_image.toJSON().url;
// Let's assign the url value to the input field
$('#image_url').val(image_url);
});
});
});
</script>
<?php
}
function awesome_wallpaper_dir( $param ){
$mydir = '/awesome';
$param['path'] = $param['path'] . $mydir;
$param['url'] = $param['url'] . $mydir;
return $param;
}
}
我找到了几个关于这个的主题,但没有解决我的问题:
我尝试在wp_enque_media()函数之前添加过滤器,在我的激活类中以及我的插件构造函数中,但是上传目录挂钩未被识别或者给出了如下错误:“上传媒体时出错......” 我用了这句话:
add_filter( 'upload_dir', array('w3pUploader', 'customize_upload_dir' ));
我希望任何人都知道我错在哪里或如何解决这个问题。
感谢您的帮助:)
答案 0 :(得分:5)
我以一种不同的方式解决了它,但它确实有效:
我在w3pUploader类中的enqueue_media()函数之后添加了这3行:
update_option('upload_path',WP_CONTENT_DIR.'/uploads/CUSTOM_FOLDER');
update_option('upload_url_path','../wp-content/uploads/CUSTOM_FOLDER');
update_option('uploads_use_yearmonth_folders', false);
在我的特殊页面上打开媒体上传器时,三个更新选项调用设置新的上传目录全局。
然后我用关闭钩子更新了Javascript部分,所以当媒体上传器以任何方式关闭时它被触发:
on('close',function() {
var data = {
action: 'reset_upload_dir'
};
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
reset_upload_dir是一个自定义的ajax函数,我在构造函数的插件主文件中通过以下方式订阅:
add_action( 'wp_ajax_reset_upload_dir', array('w3pUploader', 'reset_upload_dir' ));
作为最后一步,我在w3pUploader类中添加了reset_upload_dir函数:
public static function reset_upload_dir() {
update_option('upload_path',null);
update_option('upload_url_path',null);
update_option('uploads_use_yearmonth_folders', true);
echo "RETURN";
wp_die();
}
此reset_upload_dir函数将选项设置为null,表示wordpress使用其标准目录。
所以整个w3pUploader.php看起来像这样:
class w3pUploader {
function __construct()
{
}
public static function reset_upload_dir() {
update_option('upload_path',null);
update_option('upload_url_path',null);
update_option('uploads_use_yearmonth_folders', true);
echo "RETURN";
wp_die();
}
public static function media_upload_init() {
// jQuery
wp_enqueue_script('jquery');
// This will enqueue the Media Uploader script
wp_enqueue_media();
update_option('upload_path',WP_CONTENT_DIR.'/uploads/w3p_publications');
update_option('upload_url_path','../wp-content/uploads/w3p_publications');
update_option('uploads_use_yearmonth_folders', false);
?>
<div>
<label for="image_url">Image</label>
<input type="text" name="image_url" id="image_url" class="regular-text">
<input type="button" name="upload-btn" id="upload-btn" class="button-secondary" value="Upload Image">
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
$('#upload-btn').click(function(e) {
e.preventDefault();
var image = wp.media({
title: 'Upload Image',
// mutiple: true if you want to upload multiple files at once
multiple: false
}).open()
.on('select', function(e){
// This will return the selected image from the Media Uploader, the result is an object
var uploaded_image = image.state().get('selection').first();
// We convert uploaded_image to a JSON object to make accessing it easier
// Output to the console uploaded_image
console.log(uploaded_image);
var image_url = uploaded_image.toJSON().url;
// Let's assign the url value to the input field
$('#image_url').val(image_url);
}).on('close',function() {
var data = {
action: 'reset_upload_dir'
};
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
});
});
</script>
<?php
}
}