Opened 6 years ago
Last modified 6 years ago
#45938 new defect (bug)
Adding new term meta fails if column is wrapped in a conditional
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 5.0.2 |
Component: | Taxonomy | Keywords: | dev-feedback 2nd-opinion |
Focuses: | Cc: |
Description
I have created two custom taxonomies in my custom post type, and I have added one callback to render new columns in the taxonomy screen. Since the two taxonomies share one column, but don't share another I created something like this:
<?php add_filter( 'manage_edit-tax1_columns', 'taxonomy_additional_columns' ); add_filter( 'manage_edit-tax2_columns', 'taxonomy_additional_columns' ); function taxonomy_additional_columns( $columns ) { $columns[ 'column1' ] = esc_html__( 'Column 1', 'plugin-name' ); if ( isset( ( get_current_screen() )->taxonomy ) && ( get_current_screen() )->taxonomy === 'tax2' ) { $columns[ 'column2' ] = esc_html__( 'Column 2', 'plugin-name' ); } return $columns; } // Render content in columns add_filter( 'manage_tax1_custom_column', 'render_column_content', 10, 3 ); add_filter( 'manage_tax2_custom_column', 'render_column_content', 10, 3 ); function render_column_content( $content, $column_name, $term_id ) { switch ( $column_name ) { case... // render content from term meta } return $content; }
While this works, it has a strange bug that's related to the fact that the taxonomy terms are added using ajax and the added conditional check.
When you create a term in the second taxonomy, the term is added but the second column is empty until you refresh the page.
I haven't dug much deeper as to why this happens. I guess that the get_current_screen()
isn't available in the ajax callback or something like this.
The workaround is to create two separate functions for adding columns and duplicate the first column.