After wasting a lot of time for searching this and thought out this solution. Accessing through a category page is earlier before sidebar values are loaded. So You can make a work around by placing a placeholder div in that place and load the values in the dropdown or checkbox using Ajax & jQuery after the page is loaded. that would work for you.
Paste the following code in functions.php of your theme
// Call Actio to modify form on Add New Category
add_action( 'category_add_form_fields', 'edit_category_fields');
// Call Action to modify form on Edit Category
add_action( 'category_edit_form_fields', 'edit_category_fields');
function edit_category_fields($tag, $taxonomy)
{
// Get the Current Value if any
$leftsidebar_to_show = get_term_meta( $tag->term_id, 'leftsidebar_to_show', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="leftsidebar_to_show">Select Left Sidebar to Show</label></th>
<td>
<div id="leftsidebar_to_show_wrap">
<select name="leftsidebar_to_show" id="leftsidebar_to_show">
</select>
</div>
<!-- Store the current value as hidden input in order to Get Selected Option in jQuery -->
<input type="hidden" id="leftsidebar_to_show_val" value="<?php echo $leftsidebar_to_show; ?>" />
<!-- Category ID as hidden Input -->
<input type="hidden" name="term_id_val" value="<?php echo $tag->term_id; ?>" />
</td>
</tr>
<?php
}
// Call Actio to Save Values on Add New Category
add_action( 'edited_category', 'save_category', 10, 2);
// Call Action to Save Values on Edit Category
add_action( 'create_category', 'save_category', 10, 2);
function save_category(){
update_term_meta( $_POST['term_id_val'], 'leftsidebar_to_show', $_POST['leftsidebar_to_show']);
}
// Function to enqueue Javascript file in admin
function my_enqueue($hook) {
wp_enqueue_script( 'my_custom_script', get_template_directory_uri() . '/js/sidebars.js' );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );
// Action Function to get Sidebars through Ajax Call
function prefix_ajax_get_sidebars() {
$sidebarval = $_REQUEST['sidebarval'];
$sidebarid = $_REQUEST['sidebarid'];
$string = '<select id="'.$sidebarid.'" name="'.$sidebarid.'">';
foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) {
if($sidebarval == $sidebar['id']){
$selected = ' selected';
}else{
$selected = '';
}
$string .= '<option value="'.$sidebar['id'].'"'.$selected.'>'.$sidebar['name'].'</option>';
}
$string .= '</select>';
echo $string;
die();
}
add_action( 'wp_ajax_get_sidebars', 'prefix_ajax_get_sidebars' );
add_action('wp_ajax_nopriv_get_sidebars', 'prefix_ajax_get_sidebars');
Create a file named sidebars.js and Paste the following code
(function ($) {
"use strict";
$(document).ready(function(){
var leftsidebar_to_show_val = $('#leftsidebar_to_show_val').val();
$.post(
ajaxurl,
{
'action': 'get_sidebars',
'sidebarval': leftsidebar_to_show_val,
'sidebarid' : 'leftsidebar_to_show'
},
function(response){
$('#leftsidebar_to_show_wrap').html(response);
});
});
})(jQuery);
And move the above created file to js folder of your theme. Now you can see the sidebars listed as dropdown in the Add & Edit Category Forms.
Hope this is clear.
Thanks!