dongzhiqi0332 2016-08-22 23:52
浏览 55
已采纳

扩展wordpress插件 - 从其他插件访问变量/方法

I am trying to extend the plugin Cleverness to do list by putting my own customizations in a separate plugin, however, I'm not able to access any methods/properties from the cleverness-todo-list plugin.

There is a display() function inside the original plugin that I want to customize. I want to show the "completed" todo items as well, which, if I was just to hack the original plugin all I would need to do is add these two lines of code in the display function in the cleverness-to-do-list-frontend.class.php:

$this->list .= '<h1>Completed Todos</h1>';
$this->loop_through_todos( 1, $category );

But I'm not sure how to access $this, $this->list, or $this->loop_through_todos() from inside my own custom plugin without touching the original plugin.

I tried to just include the files where those methods/properties come from, and then call the function like I normally would, but it's not working and I'm not sure what to do.

function clever_travel_list()
{
    if ( ! is_admin() ) {
        //lines 710/711 of cleverness-to-do-list-frontend.class.php
        include_once ABSPATH . 'wp-content/plugins/cleverness-to-do-list/includes/cleverness-to-do-list.class.php';
        include_once ABSPATH . 'wp-content/plugins/cleverness-to-do-list/includes/cleverness-to-do-list-frontend.class.php';
        $this->list .= '<h1>Completed Todos</h1>';
        $this->loop_through_todos( 1, $category );
        die(ClevernessToDoList::$list);
    }

}
add_action('ctdl_list_items', clever_travel_list);

with the $this I get the following error:

Fatal error: Using $this when not in object context in /Applications/AMPPS/www/wpplugin/wp-content/plugins/clever-extension/clever-extension.php on line 11

So how do I access properties/methods when I'm extending a plugin with another plugin?

  • 写回答

1条回答 默认 最新

  • douke1891 2016-08-23 03:45
    关注

    If you have the "Cleverness to do list" plugin activated, you shouldn't need the includes. While they load the code, they're not creating or initialising the associated objects (which is why it'd be easier to have the plugin activated if possible).

    Looking at the code, cleverness-to-do-list-frontend.class.php is stored in a global variable $CTDL_Frontend_Admin once it's initialised, so something like this should work:

    function clever_travel_list()
    {
        if ( ! is_admin() ) {
            global $CTDL_Frontend_Admin;
            $CTDL_Frontend_Admin->list .= '<h1>Completed Todos</h1>';
            $CTDL_Frontend_Admin->loop_through_todos( 1, $category );
            die(ClevernessToDoList::$list);
        }
    }
    

    Though using a die() call seems wrong - was that just for debugging?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)