Fatal error when calling the plugin from ajax functions (admin-ajax.php)
-
I’ve found what I think may be an issue in the plugin. I’ve got some code on my frontend to render templates, which calls get_lazysizes_html() and normally works just fine. However, when I have the same templates set up to load through an ajax call using WordPress’s standard admin-ajax.php, I get a fatal PHP error like this:
PHP Fatal error: Uncaught Error: Call to a member function preg_replace_html() on null in /<filepath>/class-lazysizes.php:279
I found that the global $lazysizes object is not being initialized correctly because is_admin() is returning true when calling get_lazysizes_html() via admin-ajax.php, even though the code is being used to render a template on the frontend. This is happening on line 49 in class-lazysizes.php:
if ( is_admin() ) {
If I change this to the following, it works:
if ( is_admin() && ! wp_doing_ajax() ) {
Would it make sense for you to change this in the plugin, or is there a workaround I’m missing?
- The topic ‘Fatal error when calling the plugin from ajax functions (admin-ajax.php)’ is closed to new replies.