如何将媒体文件保存在特定页面的“http://example.com/custom_folder”“下

时间:2016-02-01 作者:Kaizur

我想上传一个特定文件夹中的图像,如http://example.com/custom_folder.

注意:不在http://example.com/wp-content/uploads/custom_folder/image1.jpg

3 个回复
最合适的回答,由SO网友:Jakir Hossain 整理而成

Try this

add_filter(\'upload_dir\', \'upload_image_specific_calback\');
function upload_image_specific_calback( $param ){

    //$_GET[\'post\'] which is your target post like 10 is post id.
    //After click update button.
    if(isset($_GET[\'post\'])){
        if($_GET[\'post\'] == 10){
            $param = array(
            \'path\' => get_home_path().\'logos\',
            \'url\' => home_url().\'/logos\',
            \'subdir\' => \'\',
            \'basedir\' => get_home_path(),
            \'baseurl\' => home_url(),
            \'error\' => false
            ); 
        }
    }

    //$_POST[\'post_id\'] which is your target post like 10 is post id.
    //instant upload time before save
    if(isset($_POST[\'post_id\'])){ 
        if($_POST[\'post_id\'] == 10) {
            $param = array(
            \'path\' => get_home_path().\'logos\',
            \'url\' => home_url().\'/logos\',
            \'subdir\' => \'\',
            \'basedir\' => get_home_path(),
            \'baseurl\' => home_url(),
            \'error\' => false
            );
        }   
    }

    error_log("path={$param[\'path\']}");  
    error_log("url={$param[\'url\']}");
    error_log("subdir={$param[\'subdir\']}");
    error_log("basedir={$param[\'basedir\']}");
    error_log("baseurl={$param[\'baseurl\']}");
    error_log("error={$param[\'error\']}"); 
    return $param;
}
SO网友:Jan Beck

您要使用的筛选器是upload_dir. 检查post_id 输入$_REQUEST 超级全局数组等于要对此进行限制的页面ID。然后相应地更改上载目录。

In代码:

add_filter( \'upload_dir\', \'my_uploads_dir\' );

function my_uploads_dir( $param ){

    // change this to the ID of your page
    $post_id = 135;

    // skips all uploads not associated with your page
    if ( empty( $_REQUEST[\'post_id\'] ) || $_REQUEST[\'post_id\'] != $post_id ) {
        return $param;
    }

    // set the name of your custom dir relative to WP root
    $mydir = \'awesome\';

    $param[\'path\'] = $param[\'basedir\'] = ABSPATH . $mydir;
    $param[\'url\']  = $param[\'baseurl\'] = site_url(\'/\') . $mydir;

    // ignore date-based sorting
    $param[\'subdir\'] = \'\';

    return $param;
}
在哪里$mydir 对应于WordPress安装根目录中的自定义目录。

SO网友:Aftab

将此添加到wp配置中。php文件

define( \'UPLOADS\', \'custom_folder\' );

并确保在此之前添加此代码。

require_once( ABSPATH.’wp-settings.php’ );

有关更多信息,请查看此处https://premium.wpmudev.org/blog/change-default-wordpress-uploads-folder/

谢谢