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

Twitter image url is not passed to filter for other (plugin) filter hooks #15523

Closed
vasvir opened this issue Jun 24, 2020 · 6 comments
Closed

Comments

@vasvir
Copy link

vasvir commented Jun 24, 2020

  • [x ] I've read and understood the contribution guidelines.
  • [x ] I've searched for any related issues and avoided creating a duplicate issue.

Description of what happened.

Yoast SEO works ok when the FIFU plugin is disabled (Feature Image From URL).

However when FIFU is enabled and not being used (meaning the feature image is a normal image from media library) the apply_filters() function is called with wrong arguments in wordpress-seo/src/presenters/twitter/image-presenter.php:45

Description of what is expected to happen and why.

I am expecting YOAST to pass the current feature image url as specified in wordpress but looks like the necessary info in a indirection step away in the presentation object.

Technical info

The following patch resolves the issue for me. Please note the twitter_image is under model and not directly under presentation.

Index: wp-content/plugins/wordpress-seo/src/presenters/twitter/image-presenter.php
===================================================================
--- wp-content/plugins/wordpress-seo/src/presenters/twitter/image-presenter.php (revision 21154)
+++ wp-content/plugins/wordpress-seo/src/presenters/twitter/image-presenter.php (working copy)
@@ -42,6 +42,6 @@
                 *
                 * @api string $twitter_image Image URL string.
                 */
-               return (string) \apply_filters( 'wpseo_twitter_image', $this->presentation->twitter_image, $this->presentation );
+               return (string) \apply_filters( 'wpseo_twitter_image', $this->presentation->model->twitter_image, $this->presentation );
        }
 }

Used versions

  • WordPress version: 5.4.2
  • Yoast SEO version: 14.4.1
  • Relevant plugins in case of a bug: Feature Image From URL
  • Tested with theme: ThemeTrust Create theme
@Djennez
Copy link
Member

Djennez commented Jul 6, 2020

Hi @vasvir, I am not really sure what you're expecting here.

If I explicitly set a twitter image on a post, then the wpseo_twitter_image filter has the URL of that image as the first argument. If I don't set an explicit twitter image, the filter does not get any url, but an empty string. I can still return an URL from the filter if I want to. This sounds as expected behavior to me, as no twitter image has been set initially.

We do not output a twitter:image meta tag unless a twitter image has been explicitly set.

@vasvir
Copy link
Author

vasvir commented Jul 6, 2020

Hi @Djennez, thanks for looking at it.

I may have been confused a bit.

I assume that twitter:image must have the same behavior as og:image. If that assumption is not true then I have misunderstood the scope of the filter and I apologize.

My understanding is that og:image meta tag is outputted event if a og:image is not explicitly set (in Yoast settings of the post/page)

The wpseo_opengraph_image filter outputs a url if og:image has been set but it also outputs a url if the feature image of the post has been set.

So my understanding was that the twitter filter should do the same.

@Djennez
Copy link
Member

Djennez commented Jul 7, 2020

@vasvir I've checked this with the PO's, and this is how it should work / works now:
og:image is always output if an image is available in the post. Currently, if an image is available it is filterable with wpseo_opengraph_image. But this should also be possible if no image is available (so this is a bug, it was found (here)[https://github.com//issues/2416#issuecomment-385742385] as well).

twitter:image was always set before 14.x. But since Twitter uses og:image as well, we chose to omit that tag and only output it if it's explicitly set. wpseo_twitter_image is run regardless of a set image. So it gets an empty string if no twitter image is set, and it's filterable that way.

Does this provide better insights?

@vasvir
Copy link
Author

vasvir commented Jul 7, 2020

@Djennez thanks for clearing things out.

I didn't know that:

  1. twitter works also with og:image meta tags. This certainly wasn't true last time I checked. I suppose I didn't get the memo on this one.
  2. twitter image filter is designed to have different semantics from og image filter

Thanks again for the detailed answer. It really provides better insights.

@Djennez
Copy link
Member

Djennez commented Jul 27, 2020

I'm going to close this thread as I believe all questions have been answered. The filter bug has been brought forwards as a separate issue here

@Djennez Djennez closed this as completed Jul 27, 2020
@chrissy-dev
Copy link

chrissy-dev commented Oct 10, 2020

@vasvir has a valid point and I don't believe this is solved.

We use an API to overlay our branding on images and rely heavily on rewriting the meta tags.

Our Twitter images stopped showing despite an og:image being set and working with Facebook. Furthermore, calling wpseo_twitter_image does not pass the img src as a parameter. Is this intended behaviour or a bug?

/**
* Rewrite Yoast SEO open graph image urls
*/

add_filter('wpseo_opengraph_image', 'prepend_api_for_frame'); // returns https://example.com/wp-content/themes/example/api.php?image=URL_TO_IMAGE
add_filter('wpseo_twitter_image', 'prepend_api_for_frame'); // returns empty string https://example.com/wp-content/themes/example/api.php?image=""

function prepend_api_for_frame($url) {
     return get_template_directory_uri() . "/wp-content/themes/example/api.php?image=" . $url;
}

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

No branches or pull requests

3 participants