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:
- Intercept the Wordpress actions right BEFORE an image is created
- Obtain the current image data (title, alt, description, etc)
- Change these data
- 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
.
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:
- Intercept the Wordpress actions right BEFORE an image is created
- Obtain the current image data (title, alt, description, etc)
- Change these data
- 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 Commented Sep 12, 2019 at 2:57
1 Answer
Reset to default 0If 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/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