Division by Zero error with oEmbed get requests
-
I’ve been having a problem for some time on several of my sites, but only now have been able to replicate it and get to the root cause of it.
My error logs show multiple entries like this:
PHP Warning: Division by zero in /.../wp-includes/theme-compat/embed-content.php on line 33This is infrequent and sporadic — it might occur once or twice a day at odd intervals- but each single GET request that causes it will generate about 10 lines in the error logs.
I have figured out that this occurs whenever there is a GET request to a URL like this:
https://mysite.tld/permalink/embed/
And that this in turn is a function of oEmbed in WordPress — so if another web site owner embeds a link from my site, it generates an error message.
Line 33 of embed-content.php contains this code:
if ( $data['width'] / $data['height'] > $aspect_ratio )So I am assuming the error is created when the “height” data returns a 0 value, but I don’t know what causes this. As it occurs on several different websites with different themes, I don’t think that it is caused by any sort of theme conflict.
Any ideas as to how this can be addressed? Is it a bug? or something that might be related to site configuration or use of specific plugins?
-
Hi.
Thanks for your report. It looks as if this issue has already been reported (but still is listed as a new bug) at https://core.trac.ww.wp.xz.cn/ticket/40931
Thank you for adding my report to the bug tracker — it does look like the same issue.
This doesn’t happen with every site– only some of the sites– so it could very well be related to a plugin conflict … it’s just that I haven’t been able to nail that down.
If you add a comment on that ticket (or at least “star” it), then you’ll get notifications when things happen to the ticket.
Sometimes, when people start investigating the issues, they may need to get back to the people who had a problem to get more details, or to verify that a proposed patch really seems to work.I could imaging that some themes have safeguards against declaring zero height or width, either by putting “1” as default or by making sure that a proper size always is reported.
This ticket has now been categorized as a s.c. “good-first-bug”, so we could expect that someone will pick it up, sooner or later.
Thanks, I’ll follow your suggestion and post a comment there, as I’ve done some more testing, though no answers. (Basically nothing I test in terms of de-activating plugins or themes changes anything) It happens on some, but not all, pages on some, but not all, sites.
I’d add that thanks to the information posted by the user who reported the ticket, I have now resolved the problem on my sites. It’s relatively simple. Here is how, in case anyone else runs into this issue:
1) Make a copy of the file wp-includes/theme-compat/embed-content.php
2) In the copied file, substitute this line for line 33.
Instead of
if ( $data['width'] / $data['height'] > $aspect_ratio )Substitute either
if ( ($data['width'] > 0 && $data['height'] > 0) && $data['width'] / $data['height'] > $aspect_ratio ) {or
if ( ($data['height'] > 0) && $data['width'] / $data['height'] > $aspect_ratio ) {(either line will work – it seems like cleaner coding to have the “if” statement check for both width and height, but the divide by zero error is caused only when the height value registers as 0. A width value of 0 divided by anything else would simply yield a 0 result, but not register as an error )
3) Uploaded the edited embed_content.php to the active theme, or child theme.
The template contained in the /theme-compat/ directory is used only when the active theme does not have its own template with the same name — so the fix is simply to substitute a template that addresses the bug.
The topic ‘Division by Zero error with oEmbed get requests’ is closed to new replies.