WordPress.org

Ready to get started?Download WordPress

Forums

Default custom post column to 'off' in screen options (10 posts)

  1. TCBarrett
    Member
    Posted 1 year ago #

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

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  3. TCBarrett
    Member
    Posted 1 year ago #

    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?

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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

  5. developerdeveloper
    Member
    Posted 8 months ago #

    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);
    	}    
    
    }
  6. developerdeveloper
    Member
    Posted 8 months ago #

  7. TCBarrett
    Member
    Posted 7 months ago #

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

  8. developerdeveloper
    Member
    Posted 7 months ago #

    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);
    	}    
    
    }
  9. TCBarrett
    Member
    Posted 7 months ago #

    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.

  10. bcworkz
    Member
    Posted 7 months ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.