1

On functions.php or from the UI, does WordPress provide a way to change the screen options layout value for a specific page? Right now when I make the switch from 2 columns to 1 column, it changes that for all my pages. I just need that modified for a single custom page. The id of that page is 13459.

I want to do something like this from my functions.php file. FYI the code does not work collectly (it displays the post ID as 13397 on all my pages), but hopefully it shows the point.

add_action('admin_init', 'nh_demo_hide'); function nh_demo_hide() { global $post; if (!empty($post)) { if ($post->ID == 13397) { remove_post_type_support('page', 'editor'); //add another option here to set screen options to 1 column } } } 

Thanks for any tips!

2
  • How are you making that switch currently? Share the code so we can help you. Commented Jul 26, 2017 at 19:44
  • ok, my original request is updated Commented Jul 26, 2017 at 20:06

1 Answer 1

1

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' ); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.