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 有没有复现过PFENet的,如何解决?
  • ¥15 FDMA,OFDMA,TDMA 和 CDMA 系统容量对比仿真
  • ¥15 图神经网络的入侵检测
  • ¥15 工业数据采集技术+存储架构推荐
  • ¥15 GPT-SOVITS如何训练少数民族语言支持合成语音
  • ¥20 树莓派4b使用Camera Module 3时出现the system should be configured for the legacy camera stack问题
  • ¥200 GitHub开源程序配置在VScode调试
  • ¥15 爬虫保存的scv文件0kb
  • ¥20 如何实现基于强化学习的带电作业机械臂的运动规划与控制
  • ¥15 使用wpf制作打砖块游戏时遇到的一个Bug