0

Im currently building a plugin, that adds around 15 custom columns to a custom post type listing with meta data. When registering these columns they all default to being "on" in the screen options tab. I know that whenever the user interacts with these screen options their settings are saved to the database, but is it possible to set the default for the custom columns to off, so that new users don't have to tick them off to begin with?

The following code is example code of how the registration works. In this case with a single column for simplicity..

class My_Plugin {

    private function __construct() {

        // add columns for metadata
        add_filter("manage_edit-submission_columns" , array($this, 'add_meta_columns'));

        // add data to columns
        add_action( 'manage_posts_custom_column' , array($this, 'add_meta_columns_data'), 10, 2 );

        // Make columns sortable
        add_filter( "manage_edit-submission_sortable_columns", array($this, 'make_meta_columns_sortable') );
        add_action( 'pre_get_posts', array($this, 'sorting_by_meta_column') );


    }

    function add_meta_columns($columns) {
        $offset = 2;
        $columns = array_slice($columns, 0, $offset, true) +
            array('email' => 'Email') +
            array_slice($columns, $offset, NULL, true);
        return $columns;

    }


    function add_meta_columns_data($column, $post_id){
        switch ( $column ) {
        case 'email':
            $data = get_post_meta( $post_id, 'applicant_mail', true );
            echo $data;
        break;
        }
    }

    function make_meta_columns_sortable($columns){
        $columns['email'] = 'email';
        return $columns;
    }

    function sorting_by_meta_column($query){

        if( ! is_admin() ){
            return;
        }

        $orderby = $query->get( 'orderby');

        if( 'email' == $orderby ) {
            $query->set('meta_key','applicant_mail');
            $query->set('orderby','meta_value');
        }

    }
}
  • If I understand correctly, you're trying to add options to the "Screen Options" tab right? Why don't use add_screen_option? – Mario Sep 29 '14 at 19:08
  • The options are added automatically with the meta_columns, but they defaults to 'On' – Malibur Sep 30 '14 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.