#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 (11)
#2
@
6 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.
4 years ago
#4
follow-up:
↓ 5
@
4 years 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
#5
in reply to:
↑ 4
@
4 years 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
@
4 years 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.
This ticket was mentioned in Slack in #core-editor by themiked. View the logs.
4 years ago
This ticket was mentioned in Slack in #core-editor by themiked. View the logs.
4 years ago
This ticket was mentioned in Slack in #docs by themiked. View the logs.
4 years ago
This ticket was mentioned in Slack in #docs by bedas. View the logs.
2 years ago
#11
@
23 months ago
Therefore, this is the community standard now?
Maybe https://developer.wordpress.org/reference/functions/is_admin/ should be updated?
I find it personally daunting that a CMS like WP has no method to check if you are moving around the admin area or the front end area of your website.
It is a basic thing to check.
For example, you might want to load a whole class in a plugin only in the admin area, but not in the front end (and I mean in the admin area, which does not have anything to do with AJAX calls or else, so is_admin anyway never was useful, but at least, one could work with it)
As it stands now, is_admin became very much useless to check about anything.
It may or may not return true or false, basically totally at random.
Plugins typically load at init, so that would be way too early for the fix suggested above?
Perhaps I miss some good new trick?
Could we reopen this ticket at least as a feature request to implement a proper is_admin check, even if IMO it is more of a problem introduced by Gutenberg, rather than something that needs to be added as "feature"
There's a WP_Screen::is_block_editor() method that might be helpful: