Make WordPress Core

Opened 14 months ago

Closed 9 months ago

#40931 closed defect (bug) (fixed)

Warning: Division by zero in wp-includes/theme-compat/embed-content.php on line 33

Reported by: aegis123 Owned by: SergeyBiryukov
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.7.5
Component: Embeds Keywords: good-first-bug has-patch
Focuses: Cc:


When embedding a post in another post I get the warning Warning: Division by zero in wp-includes/theme-compat/embed-content.php on line 33. It does show a embedded preview image of the post image.

if ( $data['width'] / $data['height'] > $aspect_ratio )

When change this to also check if width and height are larger than 0 the warnings go away.

if ( ($data['width'] > 0 && $data['height'] > 0) && $data['width'] / $data['height'] > $aspect_ratio ) {

Attachments (1)

40931.diff (738 bytes) - added by stevenlinx 12 months ago.
fix division by zero in embed-content.php

Download all attachments as: .zip

Change History (9)

#1 @tobifjellner
12 months ago

Another report about same issue (at least it looks like that to me): https://wordpress.org/support/topic/division-by-zero-error-with-oembed-get-requests/

#2 @SergeyBiryukov
12 months ago

  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to 4.9

#3 @Abigailm
12 months ago

I am also seeing this problem (I am the person who reported in the post linked by @tobifjellner).

It is happening with GET calls to page and post URL's ending with /embed/, on some but not all pages on a few of my sites - most don't have the problem. All sites have different themes. The error persists when disabling various plugins and also persisted when I switched one site to TwentyFifteen for testing. So no obvious conflict that I can find as the cause.

It does happen consistently on GET requests to specific pages-- that is, once I can isolate a page where it is occuring, I can trigger the error simply by browsing to (example): https:// mysite.tld/permalink/embed/ and the error will recur each time I reload the page.

I can also confirm that the code fix that @aegis123 provided above does work to resolve the problem.

Last edited 12 months ago by Abigailm (previous) (diff)

12 months ago

fix division by zero in embed-content.php

#4 @stevenlinx
12 months ago

  • Keywords has-patch added; needs-patch removed

#5 @SergeyBiryukov
10 months ago

The warning is displayed when the post has a featured image with corrupted metadata: the image has a particular size, but its height attribute is empty.

I have no objections to checking the value to avoid the warning, but it would be nice to find out what causes the metadata to become corrupted like that, as it could cause other issues elsewhere.

@aegis123, @Abigailm: Were you able to identify any pattern in the affected posts? What kind of a featured image (format, size) they have?

This ticket was mentioned in Slack in #core by sergey. View the logs.

10 months ago

#7 @Abigailm
10 months ago

I saw no pattern and have seen this error on multiple sites. It seems to be something that was triggered by bots or spiders -- that is, when I have been able to trace the error in site logs, it often was triggered by a bot such as baidu. It did seem to help to add this line to the robots.txt for the site:

User-agent: * Disallow: */embed/

Though that was simply taking a step to discourage the triggering event, rather than actually addressing the underlying problem.

And the reason that I was only seeing this with bots may simply reflect that my site content is not commonly embedded elsewhere, so real human users wouldn't ordinarily be viewing the embeds.

But the better solution for me was simply to follow the recommendation of @aegis123 and put the modified/corrected embed-content.php file in my theme's directory.

I actually am using this code on line 33:

if ( ($data['height'] > 0) && $data['width'] / $data['height'] > $aspect_ratio ) {

(Since it is only the missing height value that causes the divide by 0 error, I didn't see a need to validate the width value. If width is 0, but height is an integer, then width / height would simply produce a 0 value, which would always be < $aspect_ratio. But that's just my personal coding preference for simplicity - nothing wrong with checking both values.)

Last edited 10 months ago by Abigailm (previous) (diff)

#8 @SergeyBiryukov
9 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 41902:

Embeds: Avoid "Division by zero" warning in embed-content.php template if a thumbnail has a zero height.

Props stevenlinx, aegis123.
Fixes #40931.

Note: See TracTickets for help on using tickets.