Can't add a section to a Woocommerce Custom Setting Tab [duplicate] - woocommerce

I'm trying to add a custom settings tab to the WooCommerce settings screen. Basically I want to achieve a similar thing to the Products settings tab, with the subsections/subtabs:
I haven't been able to find any decent documentation on how to do this but I've been able to add a custom tab using this snippet:
class WC_Settings_Tab_Demo {
public static function init() {
add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
}
public static function add_settings_tab( $settings_tabs ) {
$settings_tabs['test'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
return $settings_tabs;
}
}
WC_Settings_Tab_Demo::init();
Based on what I've dug up from various threads/tutorials, I've been trying to add the sections/subtabs to the new settings tab something like this:
// creating a new sub tab in API settings
add_filter( 'woocommerce_get_sections_test','add_subtab' );
function add_subtab( $sections ) {
$sections['custom_settings'] = __( 'Custom Settings', 'woocommerce-custom-settings-tab' );
$sections['more_settings'] = __( 'More Settings', 'woocommerce-custom-settings-tab' );
return $sections;
}
// adding settings (HTML Form)
add_filter( 'woocommerce_get_settings_test', 'add_subtab_settings', 10, 2 );
function add_subtab_settings( $settings, $current_section ) {
// $current_section = (isset($_GET['section']) && !empty($_GET['section']))? $_GET['section']:'';
if ( $current_section == 'custom_settings' ) {
$custom_settings = array();
$custom_settings[] = array( 'name' => __( 'Custom Settings', 'text-domain' ),
'type' => 'title',
'desc' => __( 'The following options are used to ...', 'text-domain' ),
'id' => 'custom_settings'
);
$custom_settings[] = array(
'name' => __( 'Field 1', 'text-domain' ),
'id' => 'field_one',
'type' => 'text',
'default' => get_option('field_one'),
);
$custom_settings[] = array( 'type' => 'sectionend', 'id' => 'test-options' );
return $custom_settings;
} else {
// If not, return the standard settings
return $settings;
}
}
I've been able to add new subsections to the Products tab using similar code to the above, but it isn't working for my new custom tab. Where am I going wrong here?

1) To add a setting tab with sections, you can firstly use the woocommerce_settings_tabs_array filter hook:
// Add the tab to the tabs array
function filter_woocommerce_settings_tabs_array( $settings_tabs ) {
$settings_tabs['my-custom-tab'] = __( 'My custom tab', 'woocommerce' );
return $settings_tabs;
}
add_filter( 'woocommerce_settings_tabs_array', 'filter_woocommerce_settings_tabs_array', 99 );
2) To add new sections to the page, you can use the woocommerce_sections_{$current_tab} composite hook where {$current_tab} need to be replaced by the key slug that is set in the first function:
// Add new sections to the page
function action_woocommerce_sections_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Must contain more than one section to display the links
// Make first element's key empty ('')
$sections = array(
'' => __( 'Overview', 'woocommerce' ),
'my-section-1' => __( 'My section 1', 'woocommerce' ),
'my-section-2' => __( 'My section 2', 'woocommerce' )
);
echo '<ul class="subsubsub">';
$array_keys = array_keys( $sections );
foreach ( $sections as $id => $label ) {
echo '<li>' . $label . ' ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>';
}
echo '</ul><br class="clear" />';
}
add_action( 'woocommerce_sections_my-custom-tab', 'action_woocommerce_sections_my_custom_tab', 10 );
3) For adding the settings, as well as for processing/saving, we will use a custom function, which we will then call:
// Settings function
function get_custom_settings() {
global $current_section;
$settings = array();
if ( $current_section == 'my-section-1' ) {
// My section 1
$settings = array(
// Title
array(
'title' => __( 'Your title 1', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_1'
),
// Text
array(
'title' => __( 'Your title 1.1', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 1.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_1_text',
'css' => 'min-width:300px;'
),
// Select
array(
'title' => __( 'Your title 1.2', 'woocommerce' ),
'desc' => __( 'Your description 1.2', 'woocommerce' ),
'id' => 'custom_settings_1_select',
'class' => 'wc-enhanced-select',
'css' => 'min-width:300px;',
'default' => 'aa',
'type' => 'select',
'options' => array(
'aa' => __( 'aa', 'woocommerce' ),
'bb' => __( 'bb', 'woocommerce' ),
'cc' => __( 'cc', 'woocommerce' ),
'dd' => __( 'dd', 'woocommerce' ),
),
'desc_tip' => true,
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_1'
),
);
} elseif ( $current_section == 'my-section-2' ) {
// My section 2
$settings = array(
// Title
array(
'title' => __( 'Your title 2', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_2'
),
// Text
array(
'title' => __( 'Your title 2.2', 'text-domain' ),
'type' => 'text',
'desc' => __( 'Your description 2.1', 'woocommerce' ),
'desc_tip' => true,
'id' => 'custom_settings_2_text',
'css' => 'min-width:300px;'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_2'
),
);
} else {
// Overview
$settings = array(
// Title
array(
'title' => __( 'Overview', 'woocommerce' ),
'type' => 'title',
'id' => 'custom_settings_overview'
),
// Section end
array(
'type' => 'sectionend',
'id' => 'custom_settings_overview'
),
);
}
return $settings;
}
3.1) Add settings, via the woocommerce_settings_{$current_tab} composite hook:
// Add settings
function action_woocommerce_settings_my_custom_tab() {
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::output_fields( $settings );
}
add_action( 'woocommerce_settings_my-custom-tab', 'action_woocommerce_settings_my_custom_tab', 10 );
3.2) Process/save the settings, via the woocommerce_settings_save_{$current_tab} composite hook:
// Process/save the settings
function action_woocommerce_settings_save_my_custom_tab() {
global $current_section;
$tab_id = 'my-custom-tab';
// Call settings function
$settings = get_custom_settings();
WC_Admin_Settings::save_fields( $settings );
if ( $current_section ) {
do_action( 'woocommerce_update_options_' . $tab_id . '_' . $current_section );
}
}
add_action( 'woocommerce_settings_save_my-custom-tab', 'action_woocommerce_settings_save_my_custom_tab', 10 );
Result:
Based on:
Implement a custom WooCommerce settings page, including page sections
woocommerce/includes/admin/settings/

Related

Multiple SKU's for 1 item [duplicate]

We needed another field in our products for Prod ref/Cat numbers and I found the bit of code below which works perfectly.
I now need to add a second field for Nominal Codes used by the accountants software.
I tried using the below code again, adjusted for the new field, but it didn't work.
function jk_add_custom_sku() {
$args = array(
'label' => __( 'Custom SKU', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'id' => 'jk_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'jk_add_custom_sku' );
function jk_save_custom_meta( $post_id ) {
// grab the SKU value
$sku = isset( $_POST[ 'jk_sku' ] ) ? sanitize_text_field( $_POST[ 'jk_sku' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// save the custom SKU meta field
$product->update_meta_data( 'jk_sku', $sku );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'jk_save_custom_meta' );
Adding extra fields can be done in a very simple way:
function jk_add_custom_sku() {
$args_1 = array(
'label' => __( 'Custom SKU', 'woocommerce' ),
'placeholder' => __( 'Enter custom SKU here', 'woocommerce' ),
'id' => 'jk_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for internal use only.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args_1 );
// Extra field
$args_2 = array(
'label' => __( 'Nominal codes', 'woocommerce' ),
'placeholder' => __( 'Enter nominal codes here', 'woocommerce' ),
'id' => '_nominal_codes',
'desc_tip' => true,
'description' => __( 'This is for nominal codes.', 'woocommerce' ),
);
woocommerce_wp_text_input( $args_2 );
}
add_action( 'woocommerce_product_options_sku', 'jk_add_custom_sku' );
// Save
function jk_save_custom_meta( $product ){
if( isset($_POST['jk_sku']) ) {
$product->update_meta_data( 'jk_sku', sanitize_text_field( $_POST['jk_sku'] ) );
}
// Extra field
if( isset($_POST['_nominal_codes']) ) {
$product->update_meta_data( '_nominal_codes', sanitize_text_field( $_POST['_nominal_codes'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'jk_save_custom_meta', 10, 1 );

how to disable display custom field in page template?

I created my custom fields through code and then I registered it with this code:
/**
* Content blocks for all pages
*/
$location_page = new ACF_Group_Location;
$location_page->add_rule( 'post_type', 'page');
$location_slots = new ACF_Group_Location;
$location_slots->add_rule( 'post_type', 'slots' );
$location_card = new ACF_Group_Location;
$location_card->add_rule( 'post_type', 'card' );
ACF_Group::create( 'content-blocks', __( 'Content blocks', 'dw' ) )
->add_location( $location_page )
->add_location( $location_slots )
->add_location( $location_card )
->set_attr( 'label_placement', 'top' )
->set_attr( 'hide_on_screen', array( 'the_content', 'comments' ) )
->add_fields( array(
array(
'label' => __( 'Content blocks', 'dw' ),
'name' => 'cb',
'type' => 'flexible_content',
'button_label' => __( 'Add content block', 'dw' ),
'layouts' => $cb
),
)
)->register();
add_rule() includes:
public function add_rule( $param, $value, $operator = '==' ) {
$this->rules[] = compact( 'param', 'value', 'operator' );
return $this;
}
and my question is what code do I need to add if I wanna disable them on my page template?
So you're using ACF programatically using this
instead of adding your custom fields to all pages and then try to disable it for a particular template just add your custom field group to the template you need.
<?php
add_action( 'register_acf_groups', 'my_theme_register_acf_groups' );
function mytheme_register_acf_groups() {
ACF_Group::create( 'my_group', 'My Group' )
->add_location_rule( 'post_type', 'page' )
->add_location_rule( 'page_template', 'template-contact.php' ) // <- see here
->set_attr( 'label_placement', 'left' )
->add_fields(array(
array(
'label' => 'Test Field',
'name' => 'test_field',
'type' => 'text'
),
))
->register();
}

How can I render Nested elements on Visual Composer (WPBakery) for wordpress?

I'm trying to do some custom elements for Visual Composer plugin (WP-Bakery) for wordpress.
I have no problem with simple custom elements, but I'm trying to do some nested elements (a parent containing some child elements). I have no problems creating child elements, and if I create them alome, they are shown on wordpress, but when I try to create parent element, I can see setting elements without problem, but it's no rendered.
I think the problem is the render function (html) on parent class, but I can't get solve it.
PARENT CLASS
<?php
class vcInfoCardContainer extends WPBakeryShortCodesContainer {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'vc_infocardcontainer_mapping' ) );
add_shortcode( 'vc_infocard', array( $this, 'vc_infocardcontainer_html' ) );
}
// Element Mapping
public function vc_infocardcontainer_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('VC Info Card Container', 'ex5_vc_elements'),
'base' => 'vc_infocardcontainer',
'description' => __('Info Card Container for VC', 'ex5_vc_elements'),
'category' => __('Ex5 Elements', 'ex5_vc_elements'),
'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
'as_parent' => array('only' => 'vc_infocard'),
'is_container' => true,
'js_view' => 'VcColumnView',
'params' => array(
array(
'type' => 'textfield',
'heading' => __('Button text','ex5_vc_elements'),
'param_name' => 'button_text',
'description' => __('Default is \'Más info\'', 'ex5_vc_elements'),
'group' => 'Button',
)
),
));
}
//render
public function vc_infocard_html( $atts, $content = null ) {
// Params extraction
extract(
shortcode_atts(
array(
),
$atts
)
);
$html = '<div class="ex5-vc-info-card-container">' . do_shortcode($content) . '</div>';
return $html;
}
}
new vcInfoCardContainer();
CHILD CLASS
<?php
class vcInfoCard extends WPBakeryShortCode {
// Element Init
function __construct() {
add_action( 'init', array( $this, 'vc_infocard_mapping' ) );
add_shortcode( 'vc_infocard', array( $this, 'vc_infocard_html' ) );
}
// Element Mapping
public function vc_infocard_mapping() {
// Stop all if VC is not enabled
if ( !defined( 'WPB_VC_VERSION' ) ) {
return;
}
// Map the block with vc_map()
vc_map(
array(
'name' => __('VC Info Card', 'ex5_vc_elements'),
'base' => 'vc_infocard',
'description' => __('Info Card for VC', 'ex5_vc_elements'),
'category' => __('Ex5 Elements', 'ex5_vc_elements'),
'icon' => get_template_directory_uri().'/assets/img/vc-icon.png',
'as_child' => array('only' => 'vc_infocardcontainer'),
'params' => array(
array(
'type' => 'attach_image',
'heading' => __( 'Main image', 'ex5_vc_elements' ),
'param_name' => 'image',
'group' => 'Images',
),
array(
'type' => 'attach_image',
'heading' => __( 'Icon', 'ex5_vc_elements' ),
'param_name' => 'icon',
'group' => 'Images',
),
array(
'type' => 'colorpicker',
'heading' => __( 'Icon background color', 'ex5_vc_elements' ),
'param_name' => 'icon_background_color',
'value' => __( '#000000', 'ex5_vc_elements' ),
'group' => 'Images',
),
array(
'type' => 'textfield',
'heading' => __('Title','ex5_vc_elements'),
'param_name' => 'Title',
'group' => 'Texts',
),
array(
'type' => 'textfield',
'heading' => __( 'Text', 'ex5_vc_elements' ),
'param_name' => 'text',
'group' => 'Texts',
),
array(
'type' => 'checkbox',
'class' => 'one-third',
'heading' => __( 'Show link button', 'ex5_vc_elements' ),
'param_name' => 'show_button',
'value' => 'show',
'description' => __( 'Indicates if link button is shown)', 'ex5_vc_elements' ),
'group' => 'Button',
),
array(
'type' => 'textfield',
'heading' => __('Button text','ex5_vc_elements'),
'param_name' => 'button_text',
'description' => __('Default is \'Más info\'', 'ex5_vc_elements'),
'group' => 'Button',
),
array(
'type' => 'vc_link',
'heading' => __( 'Button link', 'ex5_vc_elements' ),
'param_name' => 'button_link',
'group' => 'Button',
),
),
));
}
//render
public function vc_infocard_html( $atts ) {
// Params extraction
extract(
shortcode_atts(
array(
'image' => '',
'icon' => '',
'icon_background_color' => '#000000',
'title' => '',
'text' => '',
'show_button' => '',
'button_text' => 'Más info',
'button_link' => '',
),
$atts
)
);
if (empty($button_text)) $button_text = __( 'Más info', 'ex5_vc_elements' );
if ($show_button === 'true') {
if (!empty($button_link)) {
$button = '<div class="ex5-vcic-button">
<a href="'. $button_link .'" target="_self" class="ex5-vcic-link" title="' . $button_text . '">
<span class="ex5-vcic-button-text">' . $button_text . '</span>
</a>
</div>';
} else {
$button = '<div class="ex5-vcic-button">
<span class="ex5-vcic-button-text">' . $button_text . '</span>
</div>';
}
} else {
$button = '';
}
$image = wp_get_attachment_image_src($image);
$icon = wp_get_attachment_image_src($icon);
//vc_build_link(
$html = '
<div class="ex5-vc-infocard">
<div class="ex5-vcic-content">
<div class="ex5-vcic-image">
<span>
<img src="' . $image[0] . '" title="history_inner_14" alt="http://oxigeno.">
</span>
</div>
<div class="ex5-vcic-icon" style="background-color: ' . $icon_background_color . '">
<img src="' . $icon[0] . '" />
</div>
<header class="ex5-vcic-headline">
<h3>' . $title . '</h3>
</header>
<div class="ex5-vcic-text">
<p>' . $text . '</p>
</div>' .
$button
. '</div>
</div>';
return $html;
}
}
new vcInfoCard();
There was a problem with the name of container shortcode. It had to be
add_shortcode( 'vc_infocardcontainer', array( $this, 'vc_infocardcontainer_html' ) );
But there's still a problem. I have a problem with do_shortcode_tag function
Attempting to parse a shortcode without a valid callback
Anybody knows how can I solve it?
I had solved it. The shortcode call was wrong because it had the wrong function name too.
public function vc_infocard_html( $atts, $content = null ) {
must be
public function vc_infocardcontainer_html( $atts, $content = null ) {

Payfast split payments

I need to add "something" to the Woocommerce payfast plugin to enable it to process split payments, as per the Payfast website it would look like this:
{ "split_payment" : { "merchant_id":10000105, "percentage":10, "amount":500, "min":100, "max":100000 } }
and it is submitted by form like this:
<input type="hidden" name="setup" value='{ "split_payment" : {
"merchant_id":10000105,
"percentage":10,
"min":100,
"max":100000}}' >
I have looked at the class-wc-gateway-payfast.php file and cannot see where this could be "injected" and I have looked for the html form and cannot find that either .... My gut tells me this is simple yet I cannot get my head around it :
The Payfast link giving me the instructions can be found here (in case I am not reading it properly) : https://developers.payfast.co.za/documentation/?html#direct-request-method
Am I not correct in my thinking that it must go here :
/**
* Generate the PayFast button link.
*
* #since 1.0.0
*/
public function generate_payfast_form( $order_id ) {
$order = wc_get_order( $order_id );
// Construct variables for post
$this->data_to_send = array(
// Merchant details
'merchant_id' => $this->merchant_id,
'merchant_key' => $this->merchant_key,
'return_url' => $this->get_return_url( $order ),
'cancel_url' => $order->get_cancel_order_url(),
'notify_url' => $this->response_url,
// Billing details
'name_first' => self::get_order_prop( $order, 'billing_first_name' ),
'name_last' => self::get_order_prop( $order, 'billing_last_name' ),
'email_address' => self::get_order_prop( $order, 'billing_email' ),
// Item details
'm_payment_id' => ltrim( $order->get_order_number(), _x( '#', 'hash before order number', 'woocommerce-gateway-payfast' ) ),
'amount' => $order->get_total(),
'item_name' => get_bloginfo( 'name' ) . ' - ' . $order->get_order_number(),
/* translators: 1: blog info name */
'item_description' => sprintf( __( 'New order from %s', 'woocommerce-gateway-payfast' ), get_bloginfo( 'name' ) ),
// Custom strings
'custom_str1' => self::get_order_prop( $order, 'order_key' ),
'custom_str2' => 'WooCommerce/' . WC_VERSION . '; ' . get_site_url(),
'custom_str3' => self::get_order_prop( $order, 'id' ),
'source' => 'WooCommerce-Free-Plugin',
);
The form gets generated based on the associative array. You would have to add the following to the array for split payments:
'setup' => json_encode(['split_payment' => ['merchant_id' => 10000105, 'percentage'=>10, 'min' => 100, 'max' => 100000]]),
The json_encode() function is used to convert the PHP multidimensional array into a JSON object. More information on the json_encode() function can be found here.
The complete array will be as follows:
$this->data_to_send = array(
// Merchant details
'merchant_id' => $this->merchant_id,
'merchant_key' => $this->merchant_key,
'return_url' => $this->get_return_url( $order ),
'cancel_url' => $order->get_cancel_order_url(),
'notify_url' => $this->response_url,
// Billing details
'name_first' => self::get_order_prop( $order, 'billing_first_name' ),
'name_last' => self::get_order_prop( $order, 'billing_last_name' ),
'email_address' => self::get_order_prop( $order, 'billing_email' ),
// Item details
'm_payment_id' => ltrim( $order->get_order_number(), _x( '#', 'hash before order number', 'woocommerce-gateway-payfast' ) ),
'amount' => $order->get_total(),
'item_name' => get_bloginfo( 'name' ) . ' - ' . $order->get_order_number(),
/* translators: 1: blog info name */
'item_description' => sprintf( __( 'New order from %s', 'woocommerce-gateway-payfast' ), get_bloginfo( 'name' ) ),
//Split Payment
'setup' => json_encode(['split_payment' => ['merchant_id' => 10000105, 'percentage'=>10, 'min' => 100, 'max' => 100000]]),
// Custom strings
'custom_str1' => self::get_order_prop( $order, 'order_key' ),
'custom_str2' => 'WooCommerce/' . WC_VERSION . '; ' . get_site_url(),
'custom_str3' => self::get_order_prop( $order, 'id' ),
'source' => 'WooCommerce-Free-Plugin',
);

Showing custom taxonomy column in custom posts type listings

I have created a custom post type named banners. Thereby I register a new taxonomy called location that specifies on which page the banner is to be shown. Everything is great however when I click on the custom posts type 'Banner' in the admin window I see all the banners created however the table does not have a column for the taxonomy 'Location'.
In other words I want to be able to see what location the banner is in, in the banners listing.
For those interested, the register_taxonomy register_taxonomy function, as of WordPress 3.5, now offers an argument for show_admin_column (false by default). Set to true, it automatically displays the taxonomy column in the admin.
You can use the manage_post-type_custom_column and manage_edit_post-type_columns filters to add your taxonomy column to the post type listing.
add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
add_filter( 'manage_edit-banner_columns', 'my_new_custom_post_column');
add_action( 'manage_banner_custom_column', 'location_tax_column_info', 10, 2);
}
function my_new_custom_post_column( $column ) {
$column['location'] = 'Location';
return $column;
}
function location_tax_column_info( $column_name, $post_id ) {
$taxonomy = $column_name;
$post_type = get_post_type($post_id);
$terms = get_the_terms($post_id, $taxonomy);
if (!empty($terms) ) {
foreach ( $terms as $term )
$post_terms[] ="<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " .esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
echo join('', $post_terms );
}
else echo '<i>No Location Set. </i>';
}
//what version of wordpress you are using
//since wp 3.5 you can pass parameter show_admin_column=>true
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );
// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( 'genre', array( 'book' ), $args );
}
In addition to Jonathan Wold answer, you can add the ability to filter by your custom taxonomy like this:
add_action('restrict_manage_posts', 'filter_post_type_backend_by_taxonomies', 99, 2);
function filter_post_type_backend_by_taxonomies($post_type, $which) {
if( 'your_post_type' !== $post_type) {
return;
}
$taxonomies = array( 'your_custom_taxonomy' );
foreach( $taxonomies as $taxonomy_slug ) {
$taxonomy_obj = get_taxonomy( $taxonomy_slug );
$taxonomy_name = $taxonomy_obj->labels->name;
$terms = get_terms($taxonomy_slug);
echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
echo '<option value="">' . sprintf( esc_html__( 'Show all %s', 'domain-name' ), $taxonomy_name) . '</option>';
foreach ( $terms as $term ) {
printf(
'<option value="%1$s" %2$s>%3$s (%4$s)</option>',
$term->slug,
( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
$term->name,
$term->count
);
}
echo '</select>';
}
}

Resources