0
votes

i got my registered sidebars listing function in functions.php everything works perfect on post options page (wp-admin/post.php) i can get all the registered sidebars via my function.

but in the category edit page (wp-admin/edit-tags.php) , i can't even access the global variable $wp_registered_sidebars , not mentioning about the function itself.

here is the function

function sidebars_list(){

global $wp_registered_sidebars;
$sidebar = array();

if (!empty($wp_registered_sidebars)):
  foreach ($wp_registered_sidebars as $key => $access):
      $sidebar[$key] = $access['name'];
  endforeach;
endif;

var_dump($sidebar);

return $sidebar;
}

as i said it works perfect on editing post and pages at the backend (frontend as well)

i tried to add_action it , no luck. can't even access the global variable in category edit page.

global $wp_registered_sidebars;
var_dump($wp_registered_sidebars);

returns empty array. but when i var_dump inside the function it returns as expected. what's wrong ?

2
I don't understand why you would want to do this. The hooks and filters running on the category edit pages are way to early, they run before the sidebars are even registered, that is why you get an empty arrayPieter Goosen
you don't understand what ? i need to do that then tell me a way to get the sidebars ?user1925151
Why do you need to have sidebars in the category edit pagePieter Goosen
i'm going to list them for custom sidebars for each categoryuser1925151

2 Answers

0
votes

As you said that the global variable value not found. I assume that your variable got overwritten the value with empty array.

I suggest you to change the variable name and retry with var_dump the variable.

global $wp_registered_sidebars_custom;
var_dump($wp_registered_sidebars_custom);
-1
votes

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!