• Hello,

    I′ve added a filter for not storing images metadata in db.

    add_filter( ‘wp_read_image_metadata’, ‘__return_false’ );

    Now I′d like to delete previously stored data but it′s serialized in wp_postmeta with meta_key value

    _wp_attachment_metadata

    This is the array:

    
    [image_meta] => Array
            (
                [aperture] => 0
                [credit] => 
                [camera] => 
                 => 
                [created_timestamp] => 0
                [copyright] => 
                [focal_length] => 0
                [iso] => 0
                [shutter_speed] => 0
                [title] => 
            )
    

    I′ve read it′s not recommended to unserialize data in WP db manually. I don′t know why. If that′s true, is there a script or any other way to unserialize and delete these data?

    Regards,

    Julia

    • This topic was modified 3 years, 4 months ago by Yui.
    • This topic was modified 3 years, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
    • This topic was modified 3 years, 4 months ago by Jose Castaneda. Reason: fixed code
Viewing 8 replies - 1 through 8 (of 8 total)
  • You don’t need to unserialize it to delete it.
    Try one of the Bulk Delete plugins.

    Thread Starter julia77

    (@julia77)

    I′ve visited your link and didn′t find any plugin related with images metadata, even the pros versions (just attachments as a whole, not differentiating arrays).

    Do you know why doing it manually is not recommended in WP db?

    The plugins I was thinking of just delete things that match what you choose, like posts or pages or post meta. I guess you are wanting to keep the post meta and just delete some parts of it.
    If you know how to read the serialized data, you can do it manually. It’s a hassle because of getting the lengths correct. Or you can write a small plugin to get the attachment data, delete the image_meta key, and save it. WP does the unserializing and serializing for you.
    https://developer.www.ads-software.com/reference/functions/wp_get_attachment_metadata/
    https://developer.www.ads-software.com/reference/functions/wp_update_attachment_metadata/
    There is also
    https://developer.www.ads-software.com/reference/functions/wp_restore_image/
    if you change your mind.

    Thread Starter julia77

    (@julia77)

    @joyously,

    Yes, I′ve seen the function. But I don′t know a safe query to run in db for already stored metadata. If you read my first post in this topic, there′s a filter but previous data is already serialized.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    You don’t need to know, or care, what the metadata is to delete it.

    Obviously make a backup first.

    Next, take a look at the postmeta table. You will find that it consists of data stored by meta_key and meta_value. You only want to delete the data with the meta_key = ‘_wp_attachment_metadata’. What the value is, well, you kinda don’t care if you’re just deleting it.

    Now, if you want to read the data and save it somewhere else first, by reformatting it in some manner, then the function you want is wp_get_attachment_metadata. And it doesn’t return serialized data to you, it returns an array of the data. Already deserialized. This is true of serialized data in general, if you use the WordPress built-in functions, then it handles that serialization stuff for you both in saving and in retrieving the data.

    In other words, don’t run an SQL query yourself, instead write some PHP code to loop through the attachment posts to get and reformat and resave your data in the way you want it, then run it in the WordPress context, as in a custom made plugin.

    Thread Starter julia77

    (@julia77)

    @otto42,

    I don′t want to delete the entire meta_value, just part of it as @joyously understood. Is there a function to extract the key array ([image_meta]? I′ve found this:

    `function maybe_unserialize( $data ) {
    if ( is_serialized( $data ) ) {
    return @unserialize( trim( $data ) );
    }

    return $data;
    }`

    What do you think?

    When you retrieve the attachment meta data with the WP function, WP deserializes it for you. You then use normal array manipulation on the data like
    unset( $data['image_data'] );
    and then use the other WP function to write that back to the database.

    Thread Starter julia77

    (@julia77)

    Hello @joyously,

    Thanks for your response and sorry about my delay.

    Could you provide this function syntaxis? For unserializing, then unsetting meta data and then writing it back to db?

    Regards,

    Julia

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Serialized data’ is closed to new replies.