Support » Plugin: Content Aware Sidebars - Unlimited Widget Areas » Get sidebar ID to check if empty

  • Resolved innovanity

    (@innovanity)



    Hi guys,
    I’ve been trying to find a method to check if a “content aware sidebar” is empty or not (if there are widgets inside).
    There are some WordPress functions that check that, like: is_active_sidebar()

    The problem is that we need sidebar ID as argument for these functions.
    Is there a way to get those ids (i.e: ca-sidebar-257836) using the custom “name” you assign to them???
    If not, any custom function to check it they are empty?

    Thanks in advance.

    https://wordpress.org/plugins/content-aware-sidebars/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Joachim Jensen

    (@intoxstudio)

    Using ca-sidebar-<id> in the function is correct. To get the actual number, go to the edit screen for a sidebar and look in the address field in your browser. You should see something like this:

    yourdomain.com/wp-admin/post.php?post=1000&action=edit

    Then call:

    is_active_sidebar("ca-sidebar-1000")

    Let me know if this works for you.

    Note that if your sidebar is replacing/merging with a host sidebar, it might be better to check if the host sidebar is empty.

    Thanks for your answer.
    I know how to check the id number manually in the panel.

    What we want, is a way to get this code given the name of the sidebar.
    We actually recieve the name of the sidebar in a var, i.e.: $sidebar_name

    So we want a way to get the id “ca-sidebar-XXXX” given the sidebar_name.
    (i.e: “my-custom-sidebar-01”)

    Thanks in advance again.

    Plugin Author Joachim Jensen

    (@intoxstudio)

    I think it would be easier for me if I knew your use case. Internally, the plugin already checks if a sidebar (or its host sidebar) is empty.

    If you already have the name/slug of the sidebar, then you can use this code:

    $args = array(
      'name'        => $sidebar_name,
      'post_type'   => 'sidebar',
      'post_status' => 'publish',
      'numberposts' => 1
    );
    $sidebars = get_posts($args);
    if( $sidebars ) {
      if(is_active_sidebar("ca-sidebar-".$sidebars[0]->ID)) {
         ...
      }
    }

    It is not pretty though and seems like an unnecessary step instead of getting the ID of the sidebar to begin with.

    This is what i needed.
    It works perfectly! Thanks a lot.

    Plugin Author Joachim Jensen

    (@intoxstudio)

    That is great!

    If you have any other questions, do not hesitate to open a new support thread here. And if you like Content Aware Sidebars, please leave a review here: https://wordpress.org/support/view/plugin-reviews/content-aware-sidebars

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get sidebar ID to check if empty’ is closed to new replies.