column name $options['artist'] = 'Artist'; return $options; } add_filter('woocommerce_csv_product_import_mapping_options', 'add_artist_column_to_importer'); /** * Add automatic mapping support for 'Custom Column'. * This will automatically select the correct mapping for columns named 'Custom Column' or 'custom column'. * * @param array $columns * @return array $columns */ function add_artist_column_to_mapping_screen($columns) { // potential column name => column slug $columns['Artist'] = 'artist'; $columns['artist'] = 'artist'; return $columns; } add_filter('woocommerce_csv_product_import_mapping_default_columns', 'add_artist_column_to_mapping_screen'); /** * Insert new taxonomy in WooCommerce importer * * @param array $parsed_data * @return array */ function woocommerce_add_artist_taxonomy($product, $data) { // set a variable with your custom taxonomy slug $custom_taxonomy = 'artist'; if (is_a($product, 'WC_Product')) { if (!empty($data[$custom_taxonomy])) { $product->save(); $custom_taxonomy_values = $data[$custom_taxonomy]; $custom_taxonomy_values = explode(",", $custom_taxonomy_values); $terms = array(); foreach ($custom_taxonomy_values as $custom_taxonomy_value) { if (!get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)) { $custom_taxonomy_args = array( 'cat_name' => $custom_taxonomy_value, 'taxonomy' => $custom_taxonomy, ); $custom_taxonomy_value_cat = wp_insert_category($custom_taxonomy_args); array_push($terms, $custom_taxonomy_value_cat); } else { $custom_taxonomy_value_cat = get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)->term_id; array_push($terms, $custom_taxonomy_value_cat); } } wp_set_object_terms($product->get_id(), $terms, $custom_taxonomy); } } return $product; } add_filter('woocommerce_product_import_pre_insert_product_object', 'woocommerce_add_artist_taxonomy', 10, 2);ID, sanitize_text_field($_POST['artist']), 'artist'); } add_action('save_post', 'save_artist'); /** * Metadata logic * * @source https://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/ */ /** * Add metadata fields to the 'Add new' page */ require('artist_add_new_meta_field.php'); add_action('artist_add_form_fields', 'artist_add_new_meta_field', 10, 2); /** * Add metadata fields to the 'Edit' page */ require('artist_edit_meta_field.php'); add_action('artist_edit_form_fields', 'artist_edit_meta_field', 10, 2); /** * Save taxonomy metadata */ function save_artist_meta($term_id) { if (isset($_POST['term_meta'])) { $t_id = $term_id; $term_meta = get_option("taxonomy_$t_id"); $cat_keys = array_keys($_POST['term_meta']); foreach ($cat_keys as $key) { if (isset($_POST['term_meta'][$key])) { $term_meta[$key] = $_POST['term_meta'][$key]; } } // Save the option array. update_option("taxonomy_$t_id", $term_meta); } } add_action('edited_artist', 'save_artist_meta', 10, 2); add_action('create_artist', 'save_artist_meta', 10, 2); /** * Enqueue media resources (required for media upload modal) */ function add_artist_admin_scripts($hook_suffix) { if (in_array($hook_suffix, ['edit-tags.php', 'term.php'])) { wp_enqueue_media(); // wp_register_script('artist-image-upload', WP_PLUGIN_URL . '/artist_taxonomy/image_upload.js', ['jquery']); // wp_enqueue_script('artist-image-upload'); } } add_action('admin_enqueue_scripts', 'add_artist_admin_scripts'); /** * Load Artist metabox CSS. */ function load_artist_metabox_styles() { echo ''; } add_action('admin_head-post.php', 'load_artist_metabox_styles'); /** * Change columns in Artist list view */ function change_artist_columns($columns) { unset($columns['description']); return $columns; } add_filter('manage_edit-artist_columns', 'change_artist_columns', 10, 1); // DISABLING THIS AS WE'RE REMOVING THE DESC COLUMN ENTIRELY. // add_filter('pre_get_terms', 'filter_if_artist', 10); function filter_if_artist($query) { if (in_array('artist', $query->query_vars['taxonomy'])) { add_filter('get_terms', 'shorten_artist_descriptions', 10, 4); } } function shorten_artist_descriptions($terms, $taxonomy, $args, $term_query) { foreach ($terms as $term) { if (strlen($term->description) > 30) { $term->description = substr($term->description, 0, 30) . '...'; } } return $terms; } add_action('admin_head-edit-tags.php', 'load_artist_columns_css'); function load_artist_columns_css() { echo ''; } column name $options['exhibition'] = 'Exhibition'; return $options; } add_filter('woocommerce_csv_product_import_mapping_options', 'add_exhibition_column_to_importer'); /** * Add automatic mapping support for 'Custom Column'. * This will automatically select the correct mapping for columns named 'Custom Column' or 'custom column'. * * @param array $columns * @return array $columns */ function add_exhibition_column_to_mapping_screen($columns) { // potential column name => column slug $columns['Exhibition'] = 'exhibition'; return $columns; } add_filter('woocommerce_csv_product_import_mapping_default_columns', 'add_exhibition_column_to_mapping_screen'); /** * Insert new taxonomy in WooCommerce importer * * @param array $parsed_data * @return array */ function woocommerce_add_exhibition_taxonomy($product, $data) { // set a variable with your custom taxonomy slug $custom_taxonomy = 'exhibition'; if (is_a($product, 'WC_Product')) { if (!empty($data[$custom_taxonomy])) { $product->save(); $custom_taxonomy_values = $data[$custom_taxonomy]; $custom_taxonomy_values = explode(",", $custom_taxonomy_values); $terms = array(); foreach ($custom_taxonomy_values as $custom_taxonomy_value) { if (!get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)) { $custom_taxonomy_args = array( 'cat_name' => $custom_taxonomy_value, 'taxonomy' => $custom_taxonomy, ); $custom_taxonomy_value_cat = wp_insert_category($custom_taxonomy_args); array_push($terms, $custom_taxonomy_value_cat); } else { $custom_taxonomy_value_cat = get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)->term_id; array_push($terms, $custom_taxonomy_value_cat); } } wp_set_object_terms($product->get_id(), $terms, $custom_taxonomy); } } return $product; } add_filter('woocommerce_product_import_pre_insert_product_object', 'woocommerce_add_exhibition_taxonomy', 10, 2 );'; } // Modify the term query to sort by custom column function sort_exhibition_terms($query) { if (!is_admin() || !isset($query->query_vars['orderby']) || $query->query_vars['orderby'] !== 'opening_date') { return $query; } $query->query_vars['meta_key'] = 'opening_date'; $query->query_vars['orderby'] = 'meta_value'; $query->query_vars['meta_type'] = 'DATE'; return $query; } do_action_ref_array( 'pre_get_terms', $query ); add_filter('pre_get_terms', 'sort_exhibition_terms'); // add_filter