Here’s a solution that will force a one column screen layout for any posts in the $one_column_layout_overrides
array. Per the code in the original question, post type support for the editor is also removed for these posts.
Screen layout options are stored in a user option for each post type. Like standard options, user option values can be filtered on the fly.
This solution uses the dynamic filter get_user_option_{$option}
, where {$option}
is the name of the user option to filter. In this case the option is named screen_layout_{$post->post_type}
where {$post->post_type}
is the post type being edited.
So, to override the user option for layout settings for the page
post type, the filter would be: get_user_option_screen_layout_page
.
Checks are in place to ensure that we only process posts in the $one_column_layout_overrides
array because we are overriding an option that applies to all posts of a particular type.
/**
* Override the Screen Options > Layout setting for list of defined posts.
* These posts will be forced to use a 1 column layout and
* will have post type support for the editor removed.
*/
add_action( 'admin_init', 'wpse_override_screen_layout_columns' );
function wpse_override_screen_layout_columns() {
// Get the ID of the current post. Bail if we can't.
$post_id = $_GET['post'];
if ( ! $post_id ) {
return;
}
// Array of post IDs that will be forced to one column layouts.
// Customize array values to fit your needs.
$one_column_layout_overrides = [
13459,
];
// Bail if this post is not one that we want to process.
if ( ! in_array( $post_id, $one_column_layout_overrides ) ) {
return;
}
// Get the post object. Bail if this fails.
$post = get_post( $post_id );
if ( ! $post ) {
return;
}
/**
* Dynamic filter for the option "screen_layout_{$post->post_type}".
*
* The dynamic portion of the hook name, `$option`, refers to the user option name.
*
* @param mixed $result Value for the user's option.
* @param string $option Name of the option being retrieved.
* @param WP_User $user WP_User object of the user whose option is being retrieved.
*/
add_filter( "get_user_option_screen_layout_{$post->post_type}",
function( $result, $option, $user ) {
// Override result to 1 column layout.
$result = 1;
return $result;
}, 10, 3
);
// Remove editor support for this post type.
remove_post_type_support( $post->post_type, 'editor' );
}