You are right; admin_init
is too early to use the global $post
variable. Fortunately, the post id is passed to the URL when editing a post.
The following code will remove post type support for a post when the _edit
meta key’s value is set to the string false. There are checks in place to ensue this only happens if a post is being edited and it’s a post
post type.
/**
* Removes support for editor when posts have the
* _edit meta key set to false.
*/
add_action( 'admin_init', 'wpse_remove_editor' );
function wpse_remove_editor() {
// Use $pagenow to determine what page is being viewed.
global $pagenow;
// Get the post ID and the action from the URL.
$the_id = isset( $_REQUEST['post'] ) && $_REQUEST['post'] ? $_REQUEST['post'] : false;
$edit_action = isset( $_REQUEST['action'] ) && 'edit' === $_REQUEST['action'] ? $_REQUEST['action'] : false;
// Bail if we're not editing a post or we can't get the post ID.
if ( 'post.php' !== $pagenow || ! $edit_action || ! $the_id ) {
return;
}
// Get the post object using the post id.
$the_post = get_post( $the_id );
// Bail if we can't get the post object, or if it's not the post post type.
if ( ! $the_post || 'post' !== $the_post->post_type ) {
return;
}
// Get value for the _edit meta key.
$use_editor = get_post_meta( $the_id, '_edit', true );
// Disable editor if value is string "false". Note strict comparison.
if ( 'false' === $use_editor ) {
remove_post_type_support( 'post' , 'editor' );
}
}