php – 在自己的插件中自定义上传WordPress Media Uploader的目录

我目前正在使用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;
}

}

我找到了几个关于这个的主题,但没有解决我的问题:

> https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_dir
> https://wordpress.stackexchange.com/questions/76895/different-upload-directory-based-on-post-type-in-a-theme/77007#77007
> https://wordpress.stackexchange.com/questions/83248/post-type-specific-upload-folder-in-3-5/83254#83254

我尝试在wp_enque_media()函数之前,在我的激活类和我的插件构造函数添加过滤器,但是上传目录挂钩无法识别或者给出了如下错误:“上传媒体时出错……”
我用过这一行:

add_filter( 'upload_dir', array('w3pUploader', 'customize_upload_dir' ));

我希望任何人都知道我错在哪里或如何解决这个问题.

谢谢你的帮助 :)

解决方法:

我以一种不同的方式解决了它,但它有效:

我在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
}
}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...