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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码