Last active
November 25, 2024 15:10
-
-
Save Clorith/3def2df9ddf47e0e7452d28cf76fb134 to your computer and use it in GitHub Desktop.
Quick drop-in snippet to disable the default full-screen editing mode and welcome guide in WordPress when a user first visits the edit interface.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wp378934573289_js_head_print() { | |
$screen = get_current_screen(); | |
// Only add script in editor views. | |
if ( 'edit' !== $screen->parent_base ) { | |
return; | |
} | |
?> | |
<script> | |
document.addEventListener( 'DOMContentLoaded', () => { | |
/** | |
* Feature statuses to enforce. | |
* | |
* $feature => $desiredState | |
* | |
*/ | |
let featureDeclaration = { | |
'fullscreenMode' : false, | |
'welcomeGuide' : false | |
}, | |
previousState = {}; | |
// Fetch any user-defined preferences. | |
const userPreference = localStorage.getItem( 'featureStatusOverrides' ); | |
if ( null !== userPreference ) { | |
featureDeclaration = JSON.parse( userPreference ); | |
} | |
// Loop over the available features that are defines as overrideable. | |
for ( const [ key, value ] of Object.entries( featureDeclaration ) ) { | |
previousState[ key ] = value; | |
// If the current editor state for the feature does not match what we expect, toggle the feature state. | |
if ( value !== wp.data.select( 'core/edit-post' ).isFeatureActive( key ) ) { | |
wp.data.dispatch( 'core/edit-post' ).toggleFeature( key ); | |
} | |
} | |
// Subscribe to feature changes in the editor. | |
wp.data.subscribe( () => { | |
let currentState; | |
// Loop over our controlled featurestot see if any of them have been set by the user. | |
for ( const [ key, value ] of Object.entries( featureDeclaration ) ) { | |
currentState = wp.data.select( 'core/edit-post' ).isFeatureActive( key ); | |
// If the preference has been changed for a setting, update the personal preferences. | |
if ( previousState[ key ] !== currentState ) { | |
previousState[ key ] = currentState; | |
} | |
} | |
// Store the custom editor preferences for this user in localStorage for next time. | |
localStorage.setItem( 'featureStatusOverrides', JSON.stringify( previousState ) ); | |
} ); | |
} ); | |
</script> | |
<?php | |
} | |
add_action( 'admin_print_footer_scripts', 'wp378934573289_js_head_print', 50 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I changed the screen check to this:
to be more specific. Now it only checks if you're editing a POST (otherwise it calls on things like tags etc).