Support » Plugins and Hacks » Hacks » Default custom post column to 'off' in screen options

  • Is it possible to add a custom column to a post type with a default setting ‘off’ (unticked) in screen options?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    The setting is saved in the database. If for example you register a post type “book” and have a custom column “book_author” you can add it to the hidden columns with something like this:

    // get the hidden columns on the edit-book screen
    // manage{edit-book}columnshidden
    $hidden_columns = get_user_option( "manageedit-bookcolumnshidden" );
    
    // get current user
    $user = wp_get_current_user();
    
    // check if the "book_author" column is already hidden
    if(!in_array('book_author', (array) $hidden_columns)){
    
      // add "book_author" to the hidden columns
      $hidden_columns[] = 'book_author';
    
      // update the hidden columns
      update_user_option($user->ID, "manageedit-bookcolumnshidden", $hidden_columns);
    }

    Not sure this is the best solution though.

    I would need to wrap that in some logical check to make sure it only happens once.

    I was sure that there were some core settings that were turned off by default in some version of WP? Or am I imagining it?

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Is your code in a plugin? Use this to only use it once when activating your plugin: http://codex.wordpress.org/Function_Reference/register_activation_hook

    I was sure that there were some core settings that were turned off by default in some version of WP? Or am I imagining it?

    I know this is true for meta boxes but not for custom columns.
    http://wordpress.org/support/topic/excerpt-discussion-author-boxes-disappeared-in-post-editing?replies=8

    Have you thought about “user_register” action?

    add_action('user_register', 'myplugin_registration_save');
    
    function myplugin_registration_save($user_id) {
    
    	// get the hidden columns on the edit-book screen
    	// manage{edit-book}columnshidden
    	$hidden_columns = get_user_option( "manageedit-bookcolumnshidden" );
    
    	// check if the "book_author" column is already hidden
    	if(!in_array('id', (array) $hidden_columns)){
    
    	  // add "book_author" to the hidden columns
    	  $hidden_columns[] = 'id';
    
    	  // update the hidden columns
    	  add_user_meta($user_id, "manageedit-bookcolumnshidden", $hidden_columns);
    	}    
    
    }

    Interesting approach. However my use case would almost always have the users registered before the functionality (plugin) is added.

    https://codex.wordpress.org/Plugin_API/Action_Reference/wp_login

    add_action('wp_login', 'myplugin_login_save');
    
    function myplugin_login_save($user_id) {
    
    	// get the hidden columns on the edit-book screen
    	// manage{edit-book}columnshidden
    	$hidden_columns = get_user_option( "manageedit-bookcolumnshidden" );
    
    	// check if the "book_author" column is already hidden
    	if(!in_array('id', (array) $hidden_columns)){
    
    	  // add "book_author" to the hidden columns
    	  $hidden_columns[] = 'id';
    
    	  // update the hidden columns
    	  add_user_meta($user_id, "manageedit-bookcolumnshidden", $hidden_columns);
    	}    
    
    }

    That would hide it every time they log in. What I’m after is for the default position to be off, rather than on. Once the user switches it on it stays on until they switch it off.

    I can work around things with, for example, some user meta to flag whether I’ve hidden it or not. But that’s not a nice solution.

    Moderator bcworkz

    (@bcworkz)

    Unfortunately, custom columns are on by default. To turn them off, an entry must be added to each user’s metadata. This could be done when the plugin is activated and for any new user registration. I’m unaware of any way to cause a column to be off by default without altering usermeta.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Default custom post column to 'off' in screen options’ is closed to new replies.