2
votes

Here is my end goal: To hide sidebars that contain no widgets which my theme currently shows as blank space.

Background: I am able to change the layout by adding/changing classes for the content and sidebar divs. The only thing that I am having problems with is getting whether there are any widgets in a particular sidebar.

Research: I have tried several different ways and I know what is preventing me from this functioning properly, but I do not know how to fix it.

Understood WordPress functions:

  • wp_get_sidebars_widgets(); - returns array of all sidebars and active widgets in each

Plugin that is throwing me off that I am trying to work with:

  • Widget Context - contains settings for showing/hiding widgets per page/post. I recommend it and it works as intended. I am trying to add an additional functionality to my site though and this hides the widgets on the page but it still shows the widgets as active in the wp_get_sidebars_widgets(); function.

Widget Context Plugin Functions

  • function check_widget_visibility( $widget_id ) { ... } - passes the widget ID and returns true or false if the widget is displayed on the page.

Here is what I have tried to call the check_widget_visibility function in the themes sidebar.php file:

//get the sidebars and active widgets and set to a variable
$sidebarActiveWidgets = wp_get_sidebars_widgets();
//set variable that will be set to true if any widgets are active for this page
$activeWidgets = false;
//'sidebar' is the name of the sidebar that I am trying to check for active widgets
foreach($sidebarActiveWidgets['sidebar'] as $widgetID){
    //check if the widget is visible
    if(check_widget_visibility( $widgetID )){
        //widget is visible set variable to true
        $activeWidgets = true;
        //widget is visible no reason to check others so break the foreach loop
        break 1;
    }
}
//check if the variable is still false
if(! $activeWidgets){

    //variable is false run additional operations to extend content and remove sidebar that contains no active functions

}

I am getting the error: Fatal error: Call to undefined function check_widget_visibility() I know that this function is called inside of a class so I tried doing a couple of different ways to call the function inside of the Widget Context Plugin class named widget_context:

  • widget_context()->check_widget_visibility( $widgetID )
    • Returns Fatal error: Call to undefined function widget_context()
  • $widget_context->check_widget_visibility( $widgetID )
    • Returns Fatal error: Call to a member function check_widget_visibility() on a non-object

Can somebody help me as to how to call a function within a theme file, where the function resided inside a plugin's class

If you need me to post more code from the Widget Context Plugin please let me know.

1

1 Answers

4
votes

So after some advanced Googling and trial and error, I figured it out:

Note: before calling a function from a plugin, it is a good idea to check and make sure the plugin is activated. To do this with this particular plugin I used

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if(is_plugin_active('widget-context/widget-context.php')){
    ...
}

Next, this widget uses a php public class that already exists, so in order to use it again, I needed to set this class as an object to a variable like this;

if(class_exists('widget_context')){
    $my_widget_context = new widget_context();
}

Next, some plugins require to load by executing a certain function. Again for the widget-context plugin, I needed:

$my_widget_context->load_plugin_settings();

Now, I could succsessfully call the plugin's classes' function correctly like:

$my_widget_context->check_widget_visibility( $widgetID )

To wrap this up, I have included the entire code for this part of my project:

...
//see if context_widget plugin is installed and activated
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if(is_plugin_active('widget-context/widget-context.php')){
    //get the sidebars and active widgets and set to a variable
    $sidebarActiveWidgets = wp_get_sidebars_widgets();
    //define variable to call context_widget class
    if(class_exists('widget_context')){
        $my_widget_context = new widget_context();
    }
    //load plugin settings
    $my_widget_context->load_plugin_settings();
    //set variable that will be set to true if any widgets are active for this page
    $activeWidgets = false;
    //loop through each widget that is activated for this sidebar
    foreach($sidebarActiveWidgets['sidebar'] as $widgetID){
        //check if the widget is visible
        if($my_widget_context->check_widget_visibility( $widgetID )){
            //widget is visible set variable to true
            $activeWidgets = true;
            //widget is visible no reason to check others so break the foreach loop
            break 1;
        }
    }
    //check if the variable is still false
    if(! $activeWidgets){
        ?>
        <script type="text/javascript">
            <!--

            //javascript here to resize main content

            //-->
        </script>
        <?php
        //variable is false run additional operations to extend content and remove sidebar that contains no active functions
    }
} else {
    //the widget-context plugin is not active so set widgets to active state
    $activeWidgets = true;
}
//use and if statement with $activeWidgets to display sidebar or not
...

I know some people will not like this because it is unique to my plugin and theme, but I know that other people can learn from the first statements to solve similar problems.