1

I have an installed plugin to import XML content, but it can't change some meta-data of the new image that will be saved.

For example, I'd like to intercept the action of saving an image BEFORE its saving and change many meta, like Title, Alt Text, Description, etc.

To be more specific, I'd like to:

  1. Intercept the Wordpress actions right BEFORE an image is created
  2. Obtain the current image data (title, alt, description, etc)
  3. Change these data
  4. Return the updated data to be saved

How to do this?


Edit 1

I've tested the following code inside my current theme's function.php:

function my_sanitize_content($data, $postarr) {
    print_r ($data);
    print_r ($postarr);
    return $data;
}
add_filter( 'wp_insert_attachment_data' , 'my_sanitize_content');

But when running the plugin it seems that wp_insert_attachment_data is not being executed, because it shows nothing.

If I use this instead:

function my_sanitize_content( $content ) {
    print_r ($content);
    return $content;
}
add_filter( 'wp_insert_post' , 'my_sanitize_content');

... the print_r shows only the current post_id.

  • You seem to be on the right track. It is almost certainly a filter/hook that you want to use but I will have to leave it to other experts to help you identify which one(s). – Matthew Brown aka Lord Matt Sep 12 at 2:57
0

If the import function you're using uses wp_insert_post() (used also by wp_insert_attachment()) to add the new images (attachment posts) to your site, then you could perhaps use the wp_insert_attachment_data( array $data, array $postarr ) filter, which is defined inside wp_insert_post().

According to the docs it,

Filters attachment post data before it is updated in or added to the database.

Parameters,

array $data    An array of sanitized attachment post data.
array $postarr An array of unsanitized attachment post data.

If you look at the wp_insert_post() docs, you can see what keys are present for the wp_insert_attachment_data parameters $data and $postarr, https://developer.wordpress.org/reference/functions/wp_insert_post/

For attachments, if I remember correctly,

  • title = post_title
  • alt = '_wp_attachment_image_alt' post_meta
  • caption = post_excerpt
  • description = post_content
  • Thanks, but debugging wp_insert_post() parameters, it's giving only the newly created post ID, nothing more... – Rogério Dec Sep 12 at 13:33
  • @RogérioDec I'm sorry, my answer was poorly worded. I mean if you look at the wp_insert_post() documentation you can see all the array keys that are in the $postarr parameter the function recieves. These same keys are, to my understanding, present in the $data and $postarr parameters the wp_insert_attachment_data filter recieves. The filter is used inside wp_insert_post() that's why I referenced the function in my answer. And it is the filter you need to use with add_filter(). – Antti Koskinen Sep 12 at 14:09
  • Thanks for your patience and attention. I've updated the original post with more information, could you see it, please? – Rogério Dec Sep 12 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.