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

Gutenberg: og:image tag does not pick the image from blocks #10501

Closed
htdat opened this issue Nov 1, 2018 · 12 comments · Fixed by #11000
Closed

Gutenberg: og:image tag does not pick the image from blocks #10501

htdat opened this issue Nov 1, 2018 · 12 comments · Fixed by #11000
Assignees
Labels
[Feature] Publicize Now Jetpack Social, auto-sharing [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack [Pri] High [Type] Bug When a feature is broken and / or not performing as intended
Milestone

Comments

@htdat
Copy link
Member

htdat commented Nov 1, 2018

Steps to reproduce the issue

Jetpack_PostImages:: from_html does not

  1. Create a post in Gutenberg
  2. Add an existing image from Media Library to an image block.
  3. Save the post and view the HTML source of the post.
  4. ERROR: og:image is using https://s0.wp.com/i/blank.jpg

If I add images through Classic Editor, there is no issue.

What I expected

og:image should use the correct image.

Debug more

I notice the differences are width and height attributes of tags when viewing the code editor:

  • Classic Editor - they do exist.
  • Gutenberg - they do not exist.

By default, the image block looks like this - the og:image tag is picked wrongly with the steps above:

<!-- wp:image {"id":139} -->
<figure class="wp-block-image"><img src="https://vu.hoangdat.blog/wp-content/uploads/2018/09/logo-1-768x767.jpg" alt="" class="wp-image-139"/></figure>
<!-- /wp:image -->

If I "hack" a bit and add width and height attributes - the og:image tag is picked correctly.

<!-- wp:image {"id":139} -->
<figure class="wp-block-image"><img src="https://vu.hoangdat.blog/wp-content/uploads/2018/09/logo-1-768x767.jpg" alt="" class="wp-image-139"/></figure>
<!-- /wp:image -->

My testing enviroment

  • WP 4.9.8
  • JP 6.6.1, Release Candidate, Bleeding Edge
  • Gutenberg 4.1.1

Possible Code

From

$image_info = jetpack_og_get_image( $image_width, $image_height );

  $image_info       = jetpack_og_get_image( $image_width, $image_height );

Then

$post_images = Jetpack_PostImages::get_images( get_the_ID(), array( 'width' => $width, 'height' => $height ) );

  	$post_images = Jetpack_PostImages::get_images( get_the_ID(), array( 'width' => $width, 'height' => $height ) );

Then

$meta = array(
'width' => (int) $image_tag->getAttribute( 'width' ),
'height' => (int) $image_tag->getAttribute( 'height' ),
);

			$meta = array(
				'width'  => (int) $image_tag->getAttribute( 'width' ),
				'height' => (int) $image_tag->getAttribute( 'height' ),
			);

@htdat htdat added [Type] Bug When a feature is broken and / or not performing as intended [Feature] Publicize Now Jetpack Social, auto-sharing [Pri] High [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack labels Nov 1, 2018
@htdat
Copy link
Member Author

htdat commented Nov 1, 2018

ticket: 1528871-zen

I am using [Pri] High as we're using Jetpack_PostImages in many places in Jetpack.

@simison
Copy link
Member

simison commented Nov 1, 2018

Like you point out, this is indeed because of Gutenberg outputting image HTML without width/height attributes:

<!-- wp:image {"id":77} -->
<figure class="wp-block-image">
  <img
     src="http://localhost/wp-content/uploads/2018/10/example.png"
     alt=""
     class="wp-image-77"
   />
</figure>
<!-- /wp:image -->

Image tags without width/height are ignored here:

// Must be larger than 200x200 (or user-specified).
if (
! $ignore_dimensions
&& (
empty( $meta['width'] )
|| empty( $meta['height'] )
|| $meta['width'] < $width
|| $meta['height'] < $height
)
) {
continue;
}

The solution could be to pick media ID from {"id":77} or wp-image-77, load media object and use that as an OG image.

cc @tyxla @ockham who are working on Gutenberg-Publicize.

@jeherve
Copy link
Member

jeherve commented Nov 1, 2018

This is a duplicate of this issue:
#10007

See my comment there for a detailed answer and a link to a Gutenberg PR that should solve the issue:
#10007 (comment)

Since there was no movement on that PR, maybe we should consider disabling our size checks for posts created with Gutenberg.

@simison
Copy link
Member

simison commented Nov 1, 2018

Since there was no movement on that PR, maybe we should consider disabling our size checks for posts created with Gutenberg.

This sounds good. Suppose FB won't penalize if we send smaller than 200px, it just won't pick it up.

@kraftbj
Copy link
Contributor

kraftbj commented Nov 1, 2018

This sounds good. Suppose FB won't penalize if we send smaller than 200px, it just won't pick it up.

It may because of Publicize. Since we have accounted for it, I haven't tried recently, but generally, if there are issues with the OG tags, Publicize will fail.

@kraftbj
Copy link
Contributor

kraftbj commented Nov 1, 2018

Gutenberg is looking to resolve lack of width/height on the front-end with work being done in the https://github.com/WordPress/gutenberg/tree/update/image-block branch.

@kristarella
Copy link

The solution could be to pick media ID from {"id":77} or wp-image-77, load media object and use that as an OG image.

This seems potentially more robust, since you can check the width of the image in the database rather than just the width that is set in the post itself. If the image in the post is < 200px, but the original is larger, the original could be used for og:image

@KokkieH
Copy link

KokkieH commented Dec 18, 2018

Noting we've had another user report of this here.

@jeherve
Copy link
Member

jeherve commented Dec 18, 2018

The solution could be to pick media ID from {"id":77} or wp-image-77, load media object and use that as an OG image.

Noting that not all images will include such a media ID; Jetpack_PostImages's from_html method (which is problematic here, it's the one that pulls images from the post content) was built to grab images that may not be in the site's media library, most often because they're hotlinked from another site. If an image is inserted into a post from the Media Library, chances are that the image was uploaded into that post, in which case Jetpack_PostImages's from_attachment would grab it even before to look at the post content.

@jeherve jeherve self-assigned this Dec 18, 2018
jeherve added a commit that referenced this issue Dec 18, 2018
Fixes #10501

- This new method parses all HTML content using WP's parse_blocks.
- We only select Core image blocks.
- We also remove any image blocks that do not include a post ID. This is because image
blocks that do not have a post ID are currently inserted using the "from image URL"
option in the image block picker. As such, all the info in that block is the image URL;
we have no data about image size for those images.
@jeherve jeherve added this to the 6.9 milestone Dec 18, 2018
@annezazu
Copy link

1638869-zen

@bikedorkjon
Copy link

1638869-zen would like a response when we have a fix

@coder-karen
Copy link
Contributor

1682946-zen

oskosk pushed a commit that referenced this issue Jan 3, 2019
…11000)

* Post Images: add new method to detect images from Gutenberg blocks

Fixes #10501

- This new method parses all HTML content using WP's parse_blocks.
- We only select Core image blocks.
- We also remove any image blocks that do not include a post ID. This is because image
blocks that do not have a post ID are currently inserted using the "from image URL"
option in the image block picker. As such, all the info in that block is the image URL;
we have no data about image size for those images.


* Use new method to get data when extracting from attachments

* Unit Tests: avoid failures for WP versions that do not support GB.

* Add support for core Gallery blocks

* Add support for Tiled Gallery block

* Return empty string when no content instead of using undefined var

* Fix the behaviour of get_post_html

- It should be able to return the post URL when possible.
- When it does not return any post content (like an empty string), it should be handled
properly.
- We should fetch the post URL and not the post title.

* Fixed block image retrieval to not return empty arrays.
  This resulted in warnings because an opengraph tag function has tried to access the 'src' attribute of an empty array. Now the block parser doesn't return an empty array in case the image data is not found or not good enough.

* Fixed one more case where false results needed to be filtered out.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Publicize Now Jetpack Social, auto-sharing [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack [Pri] High [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants