• Hello,
    I’m uploading a lot of images/photos at the moment. I use the inbuilt post gallery feature (from a create new post page). And for every image, I have to manually delete everything from the Title field.

    Is there any way to either make the Title field stay blank or (what is better for SEO and blind people) get the post title in the image Title for every image, please?

    • This topic was modified 3 years, 7 months ago by Jan Dembowski.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter GrayHunter

    (@grayhunter)

    Here is the screenshot of the Title field with the image (not post/product) title automatically put right after the uploading is completed: https://www.dropbox.com/s/0nb4w42emo32ui9/Screenshot%202022-11-22%20at%2010.31.05.png?dl=0

    Please advise.

    • This reply was modified 3 years, 7 months ago by GrayHunter.
    • This reply was modified 3 years, 7 months ago by GrayHunter.
    Moderator bcworkz

    (@bcworkz)

    Have you confirmed if your theme even uses the title field? Most do not. What’s most important for SEO and screen readers is the alt field. Both fields can be overridden when inserted, so the values used on upload are more like suggested default values.

    What issue are you hoping to solve by making the title blank? It likely makes little difference one way or the other. I think it can be made blank via filter when the image meta data is generated. If it’s really that important a need, I can investigate further from you.

    Thread Starter GrayHunter

    (@grayhunter)

    @bcworkz Thank you for your reply.

    I use Divi theme and a plugin for improving the product gallery “(Additional Variation Images Gallery for WooCommerce” by Emran Ahmed).

    The value of the Title field appears on hover on the photo: https://www.dropbox.com/s/ow02pmqegcoof7d/Screenshot%202022-11-22%20at%2015.28.34.png?dl=0

    and also there is the Title shown if you click on the magnifier: https://www.dropbox.com/s/eatzvm79cn5zkwz/Screenshot%202022-11-22%20at%2015.28.45.png?dl=0

    I believe that the best way looks like that:
    Upon uploading an image, the Title field is made blank and the alt field is filed by the product Title/Name. Is it possible?

    Moderator bcworkz

    (@bcworkz)

    Thanks for the detailed clarification. It was a lot more than I had expected 🙂

    I was thinking such data was all saved in post meta data, but it turns out most fields are saved in normal post fields of the related attachment post. The title is in post_title naturally, only the alt text is in post meta (meta key _wp_attachment_image_alt). For completeness, the description is in post_content, caption in post_excerpt, attachment slug in post_name.

    Making the title field blank makes the media list table display less useful, but at least the filename still appears, so it’s not entirely awful. The title and other post data can be altered prior the image’s attachment post being inserted through the “wp_insert_attachment_data” filter.

    Most meta data can be altered prior to insertion through the “add_post_meta” filter. I’ve not tested, but the _wp_attachment_image_alt data can very likely be altered through this filter. There may be a more specific filter somewhere, but this should do. Your callback just needs to ensure it’s only altering the intended value and not any other.

    Thread Starter GrayHunter

    (@grayhunter)

    @bcworkz Thank you for the research performed – all this sounds very exciting. I believe that there should be no pitfalls. May I ask you to help me with the code to try? I’m not very comfortable with this part of development…

    Moderator bcworkz

    (@bcworkz)

    I suggested an inappropriate meta data hook for blanking the alt field. I’m trying to blank out the data before it gets inserted, but am having trouble finding a good hook. It’s feasible to use the very generic “query” hook, but targeting the right query is an inherently weak process. Or we could blank it out after it’s inserted, except such a process is prone to race condition issues. There’s has to be a better way. I’ll dig further when I have more time and get back to you.

    As for the title, untested, but something like this:

    add_filter('wp_insert_attachment_data', 'gh_blank_title');
    function gh_blank_title( $data ) {
      $data['post_title'] = '';
      return $data;
    }
    Moderator bcworkz

    (@bcworkz)

    It looks like blanking the alt field is kind of messy. We can use the “add_post_metadata” filter, a slightly different hook than what I suggested earlier. We’d also need to use the similar “update_post_metadata” filter to prevent someone from editing the default blank value of an existing image. This does not affect any value set while inserting an image into post content.

    The way this filter is used is we return a value other than null, which short circuits the entire DB insertion routine. This means our callback is responsible for inserting into the DB and returning the field’s ID. If we called the same add_post_meta() function to save a blank value, we’d encounter the same filter hook again and cause an infinite loop. We could remove our hook so this doesn’t happen, or directly insert a DB filed using $wpdb object methods.

    This is turning out to be a lot more complicated than the ‘wp_insert_attachment_data’ data code I provided earlier. I’m unable to offer a solution for this as a quick freebie. If someone else wants to take a crack at this with the provided information, by all means do so.

    Thread Starter GrayHunter

    (@grayhunter)

    @bcworkz The code you provided works very nicely – it does the job of nulling the Title field upon uploading an image. Thank you so much for your participation! You’ve done the research and written a code to solve the issue, I appreciate this a lot!

    If you ever have some time to find a way to put a product’s title to the ALT field (of an uploading image) that would be very nice.

    Regards, Jake.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘How to update the img Title field while uploading it?’ is closed to new replies.