douqulv6059 2018-09-17 16:13 采纳率: 0%
浏览 41
已采纳

自定义上载目录不会更改附件元

I'm trying to make a download script for a password protected wordpress site. To make use of PHPs readfile() function I need to retrieve the full attachment URL based on it's ID i am passing to the download script.

I made a Custom Post Type named Downloads and also changed it's upload directory to a folder inside wp-content also named downloads.

Here is the code for it:

add_filter( 'upload_dir', 'custom_upload_directory' );

function custom_upload_directory( $args ) {

   $id = $_REQUEST['post_id'];
   $parent = get_post( $id )->post_parent;

   if( "downloads" == get_post_type( $id ) || "downloads" == get_post_type( $parent ) ) {

      $args['path'] = WP_CONTENT_DIR . '/downloads';
      $args['url']  = WP_CONTENT_URL . '/downloads';


   }
   return $args;

}

Upload works fine and when I click the link to the desired file, the ID is passed to a script via $_POST, which also works fine. But I just can't figure out a way to get the right file URL. Here's what I tried:

wp_get_attachment_url( $id );                       // returns: example.com/wp-content/uploads/html/theme/wp-content/downloads/filename.ext
wp_get_attachment_link( $id );                      // returns: <a href="example.com/wp-content/uploads/html/theme/wp-content/downloads/filename.ext">slug</a>
get_attachment_link( $id );                         // returns: example.com/downloads/file    (without .ext)
get_attached_file( $id, true );                     // returns: html/theme/wp-content/downloads/filename.ext
get_post_meta( $id, '_wp_attached_file', false );   // returns: html/theme/wp-content/downloads/filename.ext
wp_get_attachment_metadata( $id );                  // returns nothing

What I expected any of those functions to return was example.com/wp-content/downloads/filename.ext

But as you can see, some mix up the default upload directory and combine it with the new one while others just return half of the full URL (html/theme/... it's the directory the website sits on the server). So any ideas would be appreciated.

  • 写回答

1条回答 默认 最新

  • dqdmvg7332 2018-10-17 17:14
    关注

    Hours, days and even weeks later I finally found an answer and modified it to fit my needs. I came as far as displaying the right URL (the modified one) inside the file upload lightbox of Wordpress. But after publishing/updating the post it went back to the same old .../wp-content/uploads/file.ext URL.

    Someone else, somewhere else got exactly the same problem and fortunately it is said there, that you must not just alter $args['path'] and $args['url'] but you also have to alter basedir, baseurl and subdir.

    So, the complete code to change a custom post types upload directory (in this case I chose the directory .../wp-content/downloads) is the following:

    add_filter( 'upload_dir', 'change_upload_dir' );
    
    function change_upload_dir( $args ) {
    
        $id = $_REQUEST['post_id'];
    
        if( get_post_type( $id ) == 'downloads' ) {
    
          $args['basedir'] = wp_normalize_path( WP_CONTENT_DIR . 'downloads' );
          $args['baseurl'] = content_url() . '/downloads';
          $args['path']    = $args['basedir'];
          $args['url']     = $args['baseurl'];
          $args['subdir']  = '';
    
          return $args;
    
        }
    
    }
    

    So now, calling wp_get_attachment_url() finally results in something like example.com/wp-content/downloads/file.ext

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题