WordPress.org

Ready to get started?Download WordPress

Forums

How to Call to plugin's function within theme file (1 post)

  1. themaster5_07
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic