Hitet logo
  • Home
  • WordPress
    • WordPress Plugins
    • WordPress Themes
  • SEO
  • VPS
  • Hosting
  • Mac OS
  • Windows
  • IOS / iPhone
No Result
View All Result
Get Started
Hitet logo
  • Home
  • WordPress
    • WordPress Plugins
    • WordPress Themes
  • SEO
  • VPS
  • Hosting
  • Mac OS
  • Windows
  • IOS / iPhone
No Result
View All Result
hitet logo
No Result
View All Result
Home Wordpress Wordpress Plugins

WordPress Database Optimize: Clean up metadata for unexist images

admin by admin
March 27, 2022
in Wordpress Themes, Wordpress
0
Share on FacebookShare on Twitter

In process of optimizing wordpress database, cleaning up unused postmeta and metadata is a requried work. Howerver, you should have good knowledge of wordpress generally. Don’t forget: “Backup before do anything“.

ADVERTISEMENT

You might also like

Top 19 cloud web hosting uy tín nhất 2022

Top 19 cloud web hosting uy tín nhất 2022

August 5, 2022
Top 13 web hosting google uy tín nhất 2022

Top 13 web hosting google uy tín nhất 2022

August 5, 2022
clean up metadata for unexist images
clean up metadata for unexist images

PHP function cleans up metadata for images that is no longer exist in wordpress upload_basedir

Paste this code to your theme functions.php

function hitet_cleanup_attachment_metadata(){
    if ( get_option( 'hitet_clean_up_wp_attachments' ) != 'completed' ) {
        global $wpdb;
        $upload_dir   = wp_upload_dir();
        $upload_basedir = $upload_dir['basedir'];
        //use $wpdb to get all attachments in the wp_postmeta table  
        $all_attachments = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = '_wp_attachment_metadata'"
        );
        //var_dump($all_attachments);
       
        foreach( $all_attachments as $attachment ) {
            $data = unserialize($attachment->meta_value);
            $original_file_name = $data['file'] ?? '';
            $new_data = $data;
            // get all filenames from the data
            $all_sizes_filenames = wp_list_pluck( $data['sizes'], 'file' );
            foreach( $all_sizes_filenames as $size => $filename ) {
            $delete_flag = true;
                $update_flag = false;
                // check that each one doesn't exist, if it does, then don't delete the whole attachment, just update it
                // get month/year for filename
                $p = strrpos($original_file_name, '/');
                $path = ($p !== false) ? substr($original_file_name, 0, $p+1) : '';
                if ( file_exists( $upload_basedir . '/' . $path . $filename ) ){
                    $delete_flag = false;
                } else {
                    error_log($path . $filename .' DOES NOT EXIST');
                    unset( $new_data['sizes'][ $size ] );// remove that size from the array
                    $update_flag = true;
  
                }
              
            }
   
            // if there's no original file, it is not an image, so don't delete the attachment, just update it to remove the sizes
   
            if ( $original_file_name && $delete_flag ) {
  
                // none of the sized images exist, now check if original file exists
  
                if ( ! file_exists( $upload_basedir . '/' . $original_file_name ) ) {
              
                    error_log('DELETING attachment meta id ' . $attachment_id . ' :');
                    error_log(print_r($data, true));
  
                    wp_delete_attachment( $attachment_id );
  
                }
            } 
             
            if ($update_flag) {
  
                if ( count( $new_data ) === 1 && empty( $new_data['sizes'] ) ) {
                    $new_data = array();// to make sure empty _wp_attachment_metadata is deleted
                }
  
                error_log('UPDATING attachment meta id ' . $attachment_id . ' :');
                error_log(print_r($new_data, true));
  
                wp_update_attachment_metadata( $attachment_id, $new_data );
   
            }
  
  
        }// ends foreach attachment
  
  
        update_option( 'hitet_clean_up_wp_attachments', 'completed' );
      
  
    }// end run only once
  
}
  
add_action('admin_init', 'hitet_cleanup_attachment_metadata');

The code will run once when you load your wordpress admin dashboard. It will set “hitet_clean_up_wp_attachments” option to “complete” on finish. To run the function again you need to delete or modify “hitet_clean_up_wp_attachments” in your database.

admin

admin

Related Stories

Top 19 cloud web hosting uy tín nhất 2022

Top 19 cloud web hosting uy tín nhất 2022

by admin
August 5, 2022
0

Tổng hợp và liệt ra những cloud web hosting dưới đây hi vọng sẽ giúp các bạn có thêm những...

Top 13 web hosting google uy tín nhất 2022

Top 13 web hosting google uy tín nhất 2022

by admin
August 5, 2022
0

Tổng hợp và liệt ra những web hosting google dưới đây hi vọng sẽ giúp các bạn có thêm những...

Top 19 cheapest web hosting uy tín nhất 2022

Top 19 cheapest web hosting uy tín nhất 2022

by admin
August 5, 2022
0

Tổng hợp và liệt ra những cheapest web hosting dưới đây hi vọng sẽ giúp các bạn có thêm những...

Top 20 web hosting cheap uy tín nhất 2022

by admin
August 5, 2022
0

Tổng hợp và liệt ra những web hosting cheap dưới đây hi vọng sẽ giúp các bạn có thêm những...

Next Post
hover changeflickity gallery

On hover woocommerce product thumbnail change flickity gallery flatsome theme

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Independent data protection authority matters

February 14, 2022

Top 19 web hosting sites uy tín nhất 2022

August 5, 2022

Popular Story

  • clean up metadata for unexist images

    WordPress Database Optimize: Clean up metadata for unexist images

    598 shares
    Share 239 Tweet 150
  • On hover woocommerce product thumbnail change flickity gallery flatsome theme

    598 shares
    Share 239 Tweet 150
  • Can’t Create New Facebook Account: Guide To Solve Some Common Issues

    597 shares
    Share 239 Tweet 149
  • How to permanently delete attachments (photos and files) when delete a wordpress post

    591 shares
    Share 236 Tweet 148
  • Govt distributes free medicines to support self-isolating patients

    586 shares
    Share 234 Tweet 147
Hitet Blog

Hitet Blog is a place to share every thing online free.

  • Buy JNews
  • Support Forum
  • Pre-sale Question
  • Contact Us

© 2022 Hitet.net - Hitet Blog.

No Result
View All Result
  • Home
  • WordPress
    • WordPress Plugins
    • WordPress Themes
  • SEO
  • VPS
  • Hosting
  • Mac OS
  • Windows
  • IOS / iPhone

© 2022 Hitet.net - Hitet Blog.

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.