WordPress.org

Make WordPress Core

#45302 closed defect (bug) (fixed)

AJAX post but only got a 500 error - WP5.0 Beta 3

Reported by: foreverpinetree Owned by: pento
Milestone: 5.0 Priority: normal
Severity: normal Version: 5.0
Component: Editor Keywords: has-patch commit fixed-5.0
Focuses: Cc:
PR Number:

Description

My theme includes a page builder, it disables Gutenberg (use 'use_block_editor_for_post' filter) for some specified pages. It needs to connect the backend via AJAX when editing these pages, but everytime I opened the builder and only got a 500 error.

Error logs:

POST http://localhost/xampp/test/wp5.0/wp-admin/admin-ajax.php 500 (Internal Server Error)
[07-Nov-2018 01:09:58 UTC] PHP Fatal error:  Uncaught Error: Call to a member function is_block_editor() on null in D:\xampp\htdocs\xampp\test\wp5.0\wp-includes\script-loader.php:2228
Stack trace:
#0 D:\xampp\htdocs\xampp\test\wp5.0\wp-includes\class-wp-hook.php(286): wp_common_block_scripts_and_styles('')
#1 D:\xampp\htdocs\xampp\test\wp5.0\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
#2 D:\xampp\htdocs\xampp\test\wp5.0\wp-includes\plugin.php(453): WP_Hook->do_action(Array)
#3 D:\xampp\htdocs\xampp\test\wp5.0\wp-includes\script-loader.php(2074): do_action('wp_enqueue_scri...')
#4 D:\xampp\htdocs\xampp\test\wp5.0\wp-includes\class-wp-hook.php(286): wp_enqueue_scripts('')
#5 D:\xampp\htdocs\xampp\test\wp5.0\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)

How to show this problem again?

  1. Add these PHP code to functions.php to disable the gutenberg:
    <?php
    if( !function_exists('pinetree_disable_gutenberg') )
    {
            function pinetree_disable_gutenberg( $is_enabled, $post_data )
            {
                    if( $post_data->post_type === 'page' )
                    {
                            return false;
                    }
                    return $is_enabled;
            }
    }
    add_filter('use_block_editor_for_post', 'pinetree_disable_gutenberg', 10, 2);
    
  1. Add this JavaScript when dom is ready or just simply enter them into console panel:
jQuery.ajax({
			url: ajaxurl, 
			success: function(data){
				console.log('success');
			}
		});
  1. Now you will see the error from the console (I used Chrome 70).

Hope this can help.

Attachments (1)

45302.patch (749 bytes) - added by ocean90 11 months ago.

Download all attachments as: .zip

Change History (10)

#1 @foreverpinetree
12 months ago

Edit:

I just found that even I didn't disable the gutenberg, AJAX post would get the 500 error too.

#2 @SergeyBiryukov
12 months ago

  • Component changed from General to Editor
  • Milestone changed from Awaiting Review to 5.0

@ocean90
11 months ago

#3 @ocean90
11 months ago

  • Keywords has-patch commit added
  • Owner set to pento
  • Status changed from new to reviewing

Hello @foreverpinetree, welcome to WordPress Trac!

Thanks for your report. I noticed the same error on wordpress.org on sites with o2 enabled. 45302.patch removes the unnecessary is_admin() check and checks for a valid $current_screen variable.

#4 @pento
11 months ago

I've tweaked 45302.patch a little:

  • Using the global $current_screen, as get_current_screen() isn't available for front end.
  • Applied the same fix to wp_common_block_scripts_and_styles().

#5 @pento
11 months ago

  • Keywords fixed-5.0 added

#6 @pento
11 months ago

  • Status changed from reviewing to accepted

Typo-ed the ticket number, fix was committed in [43893].

#7 @pento
11 months ago

In 43894:

Script Loader: Fix an error introduced in [43893].

wp_common_block_scripts_and_styles() should only exit early if we're in the admin and the current screen isn't the block editor.

See #45302.

#8 @foreverpinetree
11 months ago

That's great! Thank you all you guys!

#9 @SergeyBiryukov
10 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 44249:

Scripts: Fix a PHP error in admin-ajax calls.

When making an ajax request, wp_enqueue_registered_block_scripts_and_styles() checked if is_admin() was true before accessing $current_screen, rather than checking if $current_screen was defined. This is usually fine, execept for in ajax requests.

Props ocean90, foreverpinetree, pento.
Merges [43893] and [43894] to trunk.
Fixes #45302.

Note: See TracTickets for help on using tickets.