WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 6 months ago

#47394 closed defect (bug) (invalid)

Gutenberg doesn't respond is_admin on firing save_post action

Reported by: SGr33n Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Editor Keywords:
Focuses: Cc:

Description

Hi,
I'm the developer of a plugin that use the action save_post inside a is_admin() condition. It's working since the first version but if I activate Gutenberg doesn't work anymore, it begin working if I move the add_action outside the is_admin condition.

Since this is a different behavior than before and I also see that on forums there are people asking why save_post doesn't work on Gutenberg (this is not true), I suppose this could be a bug.

Change History (9)

#1 @SergeyBiryukov
2 years ago

There's a WP_Screen::is_block_editor() method that might be helpful:

global $current_screen;
$is_block_editor = ( $current_screen instanceof WP_Screen ) && $current_screen->is_block_editor();

#2 @ocean90
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version 5.2.1 deleted

The block editor is using the REST API to get and save data, including posts. So you either have to check for a REST request or remove the condition altogether.

Related: https://github.com/WordPress/gutenberg/issues/11138

This ticket was mentioned in Slack in #docs by themiked. View the logs.


6 months ago

#4 follow-up: @theMikeD
6 months ago

@SergeyBiryukov your suggested fix is not working when editing a post. I get null for $current screen

public function better_is_admin() {
	if ( is_admin() ) {
		error_log( "Admin" );
		return true;
	}
	global $current_screen;
	if ( $current_screen instanceof \WP_Screen && $current_screen->is_block_editor() ) {
		error_log( "Admin" );
		return true;
	};
	error_log( "Not Admin" );
	return false;
}

Calling that on loading a block edit post results in

[20-Mar-2021 16:13:19 UTC] Admin
[20-Mar-2021 16:13:20 UTC] Admin
[20-Mar-2021 16:13:24 UTC] Admin
[20-Mar-2021 16:13:24 UTC] Admin
[20-Mar-2021 16:13:28 UTC] Not Admin
[20-Mar-2021 16:13:29 UTC] Not Admin
[20-Mar-2021 16:13:30 UTC] Not Admin
[20-Mar-2021 16:13:31 UTC] Not Admin
[20-Mar-2021 16:13:32 UTC] Not Admin
[20-Mar-2021 16:13:32 UTC] Not Admin
[20-Mar-2021 16:13:33 UTC] Not Admin
Last edited 6 months ago by theMikeD (previous) (diff)

#5 in reply to: ↑ 4 @SergeyBiryukov
6 months ago

Replying to theMikeD:

your suggested fix is not working when editing a post. I get null for $current screen

public function better_is_admin() {
	if ( is_admin() ) {
		error_log( "Admin" );
		return true;
	}
	global $current_screen;
	if ( $current_screen instanceof \WP_Screen && $current_screen->is_block_editor() ) {
		error_log( "Admin" );
		return true;
	};
	error_log( "Not Admin" );
	return false;
}

It's not quite clear when exactly your code runs. Calling it after the current screen is set, e.g. on the current_screen action or later, seems to work as expected for me.

Core itself uses the same method in a few places, e.g. in admin header, or in privacy policy guide.

#6 @theMikeD
6 months ago

@SergeyBiryukov

My mistake, I was not creating an adequately isolated test case, and was also confused about multiple calls to the same file returning different results, evidently because the block editor uses a not-completely-admin and not-completely-frontend model for displaying content in the editor. Sorry for the confusion.

Thanks to @timothybjacobs for setting me straight.

Last edited 6 months ago by theMikeD (previous) (diff)

This ticket was mentioned in Slack in #core-editor by themiked. View the logs.


6 months ago

This ticket was mentioned in Slack in #core-editor by themiked. View the logs.


6 months ago

This ticket was mentioned in Slack in #docs by themiked. View the logs.


6 months ago

Note: See TracTickets for help on using tickets.