Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically pull in alt-text from original image #71

Open
benjibee opened this issue Jul 1, 2021 · 17 comments · May be fixed by #72
Open

Automatically pull in alt-text from original image #71

benjibee opened this issue Jul 1, 2021 · 17 comments · May be fixed by #72

Comments

@benjibee
Copy link

benjibee commented Jul 1, 2021

I can't think of many use cases for having different alt-text for different image sizes, or for different aspect-ratios, for that matter. Therefore I'd suggest adding a hook directly to wp_get_attachment_image_attributes that checks for missing alt-text and attempts to set the alt-text directly so the image tag displays as expected.

Below is an example of such a function:

/**
 * Checks for a missing or empty alt tag and attempts to find the alt text
 * from the "original image" meta value provided by the ACF Image Aspect Ratio Crop plugin
 */

add_filter('wp_get_attachment_image_attributes', function ($attr, $attachment) {
    if (!isset($attr['alt']) || empty($attr['alt'])) {
        // attempt to get the ID of the original image which may contain the original alt text
        $original_id = get_post_meta($attachment->ID, 'acf_image_aspect_ratio_crop_original_image_id', true);
        $original_alt = get_post_meta($original_id, '_wp_attachment_image_alt', true);

        // assign this value to the alt key (will be empty if either get_post_meta call above fails)
        $attr['alt'] = $original_alt;
    }

    return $attr;
}, 10, 2);
@dylantuohy
Copy link

hi @benjibee maybe you will be interested in this solution which copies the alt text of the original when the new cropped image is created: #58

@benjibee
Copy link
Author

benjibee commented Jul 5, 2021

Hey @dylantuohy thanks for the suggestion. That would actually create another issue as far as I'm concerned, namely duplicate contact that would then have to be updated if the original changes.

WordPress currently shares alt-texts between image sizes because logically there are very few reasons (outside of very specific art-direction) that you'd want different alt-text for different images, so I can't see why one would want to copy text over when it should actually remain connected to the original image's meta data.

@joppuyo
Copy link
Owner

joppuyo commented Oct 21, 2021

Hey, thanks for the PR. I'm thinking of including this feature in the next major version of the plugin. I like this approach better than copying the values.

I'll probably add an option to enable and disable this functionality.

I have just a few questions.

With the wp_get_attachment_image_attributes filter, where are the values visible? Are they visible when using get_field ? Are they visible in the WordPress image edit modal?

It's possible to overwrite the values so that cropped image has different meta information than the original, right?

@benjibee
Copy link
Author

Hey @joppuyo I'm glad to hear this might be helpful!

From my experience so far, it looks like wp_get_attachment_image_attributes pretty much filters the caption everywhere. I've since stopped using this plugin, however, so I can't test it right away. If you don't have time to test it yourself I can setup a test case in the coming weeks.

As for overwriting the values, it should only attempt to fecth the original alt if no alt text is present on the cropped image, so that shouldn't be an issue at all.

@mbateam
Copy link

mbateam commented Aug 8, 2022

Wordpress uses the same photo data when it has users crop images. Since the image topic is typically the same or similar the user would appreciate the head start on editing what was there. Please add this feature.
https://developer.wordpress.org/reference/functions/wp_ajax_crop_image/

@mbateam
Copy link

mbateam commented Aug 8, 2022

I personally need the description for my purposes and would appreciate this.

@joppuyo
Copy link
Owner

joppuyo commented Aug 13, 2022

Hey, unfortunately I haven’t had very much time lately so progress on the plugin has been a bit slow. However, right now I’m focused on the new version 7.0.0 of the plugin which is a partial rewrite of the plugin.

The current code of the plugin is a bit of a mess and after the version 7.0.0 is complete, it will be much easier to add new features to the plugin. I’m pretty sure I will be able to complete the rewrite in upcoming months and after I can focus on new features such as this.

I acknowledge that it would be nice to have this feature as soon as possible but it wouldn’t make much sense to add features to the current codebase because it’s in need of major improvement right now.

@mbateam
Copy link

mbateam commented Aug 13, 2022 via email

@joppuyo
Copy link
Owner

joppuyo commented Aug 14, 2022

I will be updating this ticket when there’s progress on this issue so you should get a notification since you are participating in this issue.

@mbateam
Copy link

mbateam commented Aug 24, 2022

I will be updating this ticket when there’s progress on this issue so you should get a notification since you are participating in this issue.

In your development on the new version. Are you creating a feature for front end editing like this: https://fengyuanchen.github.io/cropperjs/ where the user does not have to leave the page to crop. Right now users have to go to the backend, open media gallery or modal window click multiple times then save and then revisit the page to get a look at what they did. This is way to cumbersome. It would be great if the user could have a visual of the entire page on the front end and see how it looks with the proposed cropped version, before actually cropping the image. If the user doesn't like how it looks they can reset the cropped view to what was there before, then when they like the look click crop. There is no reason to have to restore an image to previous state until after they actually crop it. The open in modal or visit the media gallery and click multiple times save and then revisit the page is way to cumbersome.

@joppuyo
Copy link
Owner

joppuyo commented Aug 24, 2022

In your development on the new version. Are you creating a feature for front end editing like this

Hey, this kind of functionality sounds so complicated it's out of scope for this plugin.

Have you tried to use this plugin in conjunction with ACF blocks? This would allow you to see the field controls in the sidebar and a preview of the element inside the block editor.

@mbateam
Copy link

mbateam commented Aug 24, 2022

I did not but will check it out. Could you allow users to crop on the page instead of having a pop-up screen?

@joppuyo
Copy link
Owner

joppuyo commented Aug 24, 2022

I did not but will check it out. Could you allow users to crop on the page instead of having a pop-up screen?

Since this is an ACF field, I'm limited by the way ACF works. That's why it's not possible to build this sort of integration with the front end.

I'm not planning on moving the cropper "inline" on the page because there simply isn't enough space for it. The modal allows the image to take up the entire screen which makes the cropping process easier.

I think ACF blocks would be your best bet for previewing how the image looks in the context of a particular element.

@mbateam
Copy link

mbateam commented Aug 24, 2022

I did not but will check it out. Could you allow users to crop on the page instead of having a pop-up screen?

Since this is an ACF field, I'm limited by the way ACF works. That's why it's not possible to build this sort of integration with the front end.

I'm not planning on moving the cropper "inline" on the page because there simply isn't enough space for it. The modal allows the image to take up the entire screen which makes the cropping process easier.

I think ACF blocks would be your best bet for previewing how the image looks in the context of a particular element.

Is this the resource on how to implement this solution: https://www.advancedcustomfields.com/resources/blocks/

@joppuyo
Copy link
Owner

joppuyo commented Aug 24, 2022

Is this the resource on how to implement this solution: https://www.advancedcustomfields.com/resources/blocks/

Yes, this is the functionality I'm talking about. If you need more support with this, I would recommend opening another ticket since this isn't related to the alt text that this issue is about.

@mbateam
Copy link

mbateam commented Aug 24, 2022

Done, sorry for making a mess!

@jameelmoses
Copy link

As pointed out here, this could cause more issues than it solves in my opinion. We always have original_image to access the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants