php - 如何在没有整个插件的情况下调用WordPress插件功能

标签 php class wordpress

这是我的最终目标:隐藏不包含小部件的侧边栏,而我的主题当前将其显示为空白。

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

研究:我尝试了几种不同的方法,我知道是什么阻止了我正常运行,但我不知道如何解决它。

了解 WordPress 功能:

  • wp_get_sidebars_widgets(); - 返回所有侧边栏和每个侧边栏中事件小部件的数组

我正在尝试使用的插件让我感到厌烦:

  • Widget Context - 包含显示/隐藏每页/帖子小部件的设置。我推荐它并且它按预期工作。我正在尝试向我的网站添加附加功能,这会隐藏页面上的小部件,但它仍然在 wp_get_sidebars_widgets(); 函数中显示小部件处于事件状态。

小组件上下文插件函数

  • function check_widget_visibility( $widget_id ) { ... } - 传递小部件 ID,如果小部件显示在页面上,则返回 true 或 false。

这是我尝试在主题 sidebar.php 文件中调用 check_widget_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

}

我收到错误: fatal error :调用未定义的函数 check_widget_visibility() 我知道这个函数是在类内部调用的,所以我尝试了几种不同的方法来调用该函数在名为 widget_context 的 Widget Context Plugin 类内部:

  • widget_context()->check_widget_visibility( $widgetID )
    • 返回 fatal error :调用未定义的函数 widget_context()
  • $widget_context->check_widget_visibility( $widgetID )
    • 返回 fatal error :在非对象上调用成员函数 check_widget_visibility()

有人可以帮助我如何调用主题文件中的函数,该函数驻留在插件的类中

如果您需要我从 Widget Context 插件发布更多代码,请告诉我。

最佳答案

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

注意:在从插件调用函数之前,最好检查一下 make sure the plugin is activated 。为此,我使用了这个特定的插件

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

接下来,这个小部件使用了一个已经存在的 php 公共(public)类,因此为了再次使用它,我需要将此类设置为像这样的变量的对象;

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

我知道有些人不会喜欢这个,因为它是我的插件和主题所独有的,但我知道其他人可以从第一个语句中学习来解决类似的问题。

关于php - 如何在没有整个插件的情况下调用WordPress插件功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17128950/

相关文章:

PHP 数组警告 : Creating default object from empty value

使用默认值时 switch 语句中的 C# 'Not all code paths return a value'

java - AdapterView 是原始类型。对泛型类型 AdapterView<T> 的引用应该被参数化

php - WordPress 检查查询中的空数据

css - 这张表是否不允许 CSS 正常运行

PHP 生成的图表。 CSS 错误。刻度错误。 SVG-多边形

php - 使用 Angular 从 mysql 自动重新加载表

php - 如何使用来自 3rd 方站点的 URL 端点进行 JSON 响应

类中的Android Gesture Detection,调用方法

javascript - 将 WordPress 帖子 ID 传递到另一个页面