Php 如何在没有整个插件的情况下调用WordPress插件函数

Php 如何在没有整个插件的情况下调用WordPress插件函数,php,class,wordpress,Php,Class,Wordpress,这是我的最终目标:隐藏没有小部件的边栏,我的主题当前显示为空白 背景:我可以通过添加/更改内容和边栏div的类来更改布局。我唯一有问题的是在一个特定的边栏中是否有任何小部件 研究:我尝试了几种不同的方法,我知道是什么阻止了我正常工作,但我不知道如何修复它 了解WordPress的功能: wp_get_sidebars_widgets()-返回每个窗口中所有侧栏和活动小部件的数组 我正在尝试使用的插件会让我感到厌烦: -包含每页/每篇文章显示/隐藏小部件的设置。我推荐它,它可以按预期工作。虽

这是我的最终目标:隐藏没有小部件的边栏,我的主题当前显示为空白

背景:我可以通过添加/更改内容和边栏div的类来更改布局。我唯一有问题的是在一个特定的边栏中是否有任何小部件

研究:我尝试了几种不同的方法,我知道是什么阻止了我正常工作,但我不知道如何修复它

了解WordPress的功能:

  • wp_get_sidebars_widgets()-返回每个窗口中所有侧栏和活动小部件的数组
我正在尝试使用的插件会让我感到厌烦:

  • -包含每页/每篇文章显示/隐藏小部件的设置。我推荐它,它可以按预期工作。虽然我正在尝试向我的站点添加一个附加功能,这会隐藏页面上的小部件,但它仍然会在
    wp_get_sidebars_widgets()中显示活跃的小部件函数
小部件上下文插件函数

  • function check\u widget\u visibility($widget\u id){…}
    -传递小部件id,如果小部件显示在页面上,则返回true或false
下面是我在themes sidebar.php文件中尝试调用的
check\u widget\u visibility
函数:

//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

}
我得到了错误:
致命错误:调用未定义的函数check\u widget\u visibility()
我知道这个函数是在一个类中调用的,所以我尝试了几种不同的方法来调用名为
widget\u Context
的widget Context插件类中的函数:

  • widget\u context()->检查widget\u可见性($widgetID)
    • 返回
      致命错误:调用未定义的函数widget_context()
  • $widget\u context->check\u widget\u可见性($widgetID)
    • 返回
      致命错误:对非对象调用成员函数check\u widget\u visibility()
有人能帮助我如何在一个主题文件中调用一个函数吗


如果您需要我发布更多来自Widget上下文插件的代码,请告诉我。

因此,经过一些高级的谷歌搜索和反复试验,我发现:

注意:在从插件调用函数之前,最好先检查并确认。为了实现这一点,我使用了这个特殊的插件

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if(is_plugin_active('widget-context/widget-context.php')){
    ...
}
接下来,这个小部件使用了一个已经存在的php公共类,所以为了再次使用它,我需要将这个类设置为一个对象,比如这样的变量

if(class_exists('widget_context')){
    $my_widget_context = new widget_context();
}
接下来,一些插件需要通过执行某个函数来加载。同样,对于小部件上下文插件,我需要:

$my_widget_context->load_plugin_settings();
现在,我可以成功地正确调用插件的类函数,如:

$my_widget_context->check_widget_visibility( $widgetID )
总而言之,我已经包含了我项目这一部分的全部代码:

...
//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
...
。。。
//查看是否安装并激活了context_小部件插件
include_once(ABSPATH.'wp admin/includes/plugin.php');
if(插件是否处于活动状态('widget-context/widget-context.php')){
//获取侧栏和活动小部件并设置为变量
$sidebarActiveWidgets=wp_get_边栏_widgets();
//定义变量以调用上下文\小部件类
如果(类_存在('widget_上下文')){
$my_widget_context=新widget_context();
}
//加载插件设置
$my_widget_context->load_plugin_settings();
//设置变量,如果此页面的任何小部件处于活动状态,则该变量将设置为true
$activeWidgets=false;
//循环浏览为此侧栏激活的每个小部件
foreach($sidebarActiveWidgets['sidebar']作为$widgetID){
//检查小部件是否可见
if($my\u widget\u context->check\u widget\u可见性($widgetID)){
//小部件可见,请将变量设置为true
$activeWidgets=true;
//小部件是可见的,没有理由检查其他部件,因此请中断foreach循环
破口1;
}
}
//检查变量是否仍然为false
如果(!$activeWidgets){
?>

因此,经过一些高级谷歌搜索和反复试验,我发现:

注意:在从插件调用函数之前,最好检查和。要使用我使用的这个特定插件执行此操作

include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if(is_plugin_active('widget-context/widget-context.php')){
    ...
}
接下来,这个小部件使用了一个已经存在的php公共类,所以为了再次使用它,我需要将这个类设置为一个对象,比如这样的变量

if(class_exists('widget_context')){
    $my_widget_context = new widget_context();
}
接下来,一些插件需要通过执行某个函数来加载。同样,对于小部件上下文插件,我需要:

$my_widget_context->load_plugin_settings();
现在,我可以成功地正确调用插件的类函数,如:

$my_widget_context->check_widget_visibility( $widgetID )
总而言之,我已经包含了我项目这一部分的全部代码:

...
//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
...
。。。
//查看是否安装并激活了context_小部件插件
include_once(ABSPATH.'wp admin/includes/plugin.php');
if(插件是否处于活动状态('widget-context/widget-context.php')){
//获取侧栏和活动小部件并设置为变量
$sidebarActiveWidgets=wp_get_边栏_widgets();
//定义变量以调用上下文\小部件类
如果(类_存在('widget_上下文')){
$my_widget_context=新widget_context();
}
//加载插件设置
$my_widget_context->load_plugin_settings();
//设置变量,如果此页面的任何小部件处于活动状态,则该变量将设置为true
$activeWidgets=false;
//循环浏览为此侧栏激活的每个小部件
foreach($sidebarActiveWidgets['sidebar']作为$widgetID){
//检查小部件是否可见
if($my\u widget\u context->check\u widget\u可见性($widgetID)){
//小部件可见,请将变量设置为true
$activeWidgets=true;
//小部件是可见的,没有理由检查其他部件,因此请中断foreach循环
破口1;
}
}
//检查变量是否为st