这里有一个解决方案,它将强制在$one_column_layout_overrides
大堆根据原始问题中的代码,这些帖子也删除了对编辑器的帖子类型支持。
屏幕布局选项存储在user option 针对每种岗位类型。与标准选项一样,可以动态过滤用户选项值。
此解决方案使用动态过滤器get_user_option_{$option}
, 哪里{$option}
要筛选的用户选项的名称。在这种情况下,选项命名为screen_layout_{$post->post_type}
哪里{$post->post_type}
是正在编辑的帖子类型。
因此,要覆盖的布局设置的用户选项page
post类型,过滤器应为:get_user_option_screen_layout_page
.
检查到位,以确保我们只处理$one_column_layout_overrides
数组,因为我们正在重写一个应用于特定类型的所有帖子的选项。
/**
* 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\' );
}