php - Change upload URL by mime type

I'm working on a site that has several hundred pdf files uploaded over the course of a few months. All uploads are

I'm working on a site that has several hundred pdf files uploaded over the course of a few months. All uploads are organised in the uploads directory by year/month.

The client has just decided they don't want dates in their pdf urls. Instead of having the URL site/app/uploads/2019/06/whatever.pdf they want the url site/app/uploads/pdf/whatever.pdf. Note that this only applies for pdf files.

For new uploads this is easy enough. I added a filter for wp_handle_upload_prefilter that checks if the extension is pdf and changes the upload directory to uploads/pdf accordingly. However I'm not sure how to change the URL for all existing pdfs.

I looked into moving all pdfs into the uploads/pdf directory and then modifying the wp_posts GUID field to reflect the new location with

UPDATE
  `wp_posts`
SET
  guid = CONCAT(
    SUBSTRING_INDEX(guid, '/uploads', 1),
    '/uploads/pdf/',
    SUBSTRING_INDEX(guid, '/', -1)
  )
WHERE
  post_mime_type = 'application/pdf'

..but this didn't work. (done on a test site). The GUIDs were changed but the URLs remained the same.

How can I update the URLs of all pdf uploads without modifying URLs of any other upload type?

I'm working on a site that has several hundred pdf files uploaded over the course of a few months. All uploads are organised in the uploads directory by year/month.

The client has just decided they don't want dates in their pdf urls. Instead of having the URL site/app/uploads/2019/06/whatever.pdf they want the url site/app/uploads/pdf/whatever.pdf. Note that this only applies for pdf files.

For new uploads this is easy enough. I added a filter for wp_handle_upload_prefilter that checks if the extension is pdf and changes the upload directory to uploads/pdf accordingly. However I'm not sure how to change the URL for all existing pdfs.

I looked into moving all pdfs into the uploads/pdf directory and then modifying the wp_posts GUID field to reflect the new location with

UPDATE
  `wp_posts`
SET
  guid = CONCAT(
    SUBSTRING_INDEX(guid, '/uploads', 1),
    '/uploads/pdf/',
    SUBSTRING_INDEX(guid, '/', -1)
  )
WHERE
  post_mime_type = 'application/pdf'

..but this didn't work. (done on a test site). The GUIDs were changed but the URLs remained the same.

How can I update the URLs of all pdf uploads without modifying URLs of any other upload type?

Share Improve this question asked Jun 7, 2019 at 0:48 jlajla 1338 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It turns out the value that points to the PDFs' locations on disk is stored in the wp_postmeta table, under the meta_value field.

For example a media item located at site/app/uploads/2019/06/whatever.pdf would have a meta_value value of 2019/06/whatever.pdf. To change the location of all PDFs I first found the IDs of all PDFs (from the ID column in the wp_posts table, selecting where post_mime_type='application/pdf) and then, for each of those IDs, found the corresponding row in the wp_postmeta table using the post_id column and updated that row's meta_value field, replacing the year/month/ with pdf/.

I used a PHP script:

$ids = $wpdb->get_results( "SELECT * FROM `wp_posts` WHERE post_mime_type='application/pdf'" );
foreach ( $ids as $id ) {
    $pdf = $wpdb->get_results( "SELECT * FROM `wp_postmeta` WHERE post_id=$id->ID" );
    $oldpdf = $pdf[0]->meta_value;

    $_tmp = explode("/",  $pdf[0]->meta_value);
    if ( sizeof($_tmp) > 2 ) {
        unset( $_tmp[0], $_tmp[1] );
        $newpdf = 'pdf/'.implode("/", $_tmp);
    }
    else {
      $newpdf = $oldpdf;
    }

    if ( $oldpdf != $newpdf ) {
        rename( wp_get_upload_dir()['basedir'].'/'.$oldpdf, wp_get_upload_dir()['basedir'].'/'.$newpdf );
        $wpdb->update(
            'wp_postmeta',
            [ 'meta_value' => $newpdf ],
            [ 'post_id' => $id->ID ]
        );
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745389887a4625612.html

相关推荐

  • php - Change upload URL by mime type

    I'm working on a site that has several hundred pdf files uploaded over the course of a few months. All uploads are

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信