最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hooks - How to change image atributes right before an image to be saved?

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Sep 12, 2019 at 14:34 Rogério Dec asked Sep 11, 2019 at 19:41 Rogério DecRogério Dec 1993 silver badges12 bronze badges 1
  • 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
Add a comment  | 

1 Answer 1

Reset to default 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/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
发布评论

评论列表(0)

  1. 暂无评论