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 C语言设计一个简单的自动换档程序
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。