我正试图在我的wordpress插件中使用以下github项目:
PHP Google图表API 的对象包装器
我尝试了以下方法:
require_once(WPPLUGIN_PATH. "gchart/gChartInit.php");
$piChart = new gPieChart(); // <---- here I get the error
$piChart->addDataSet(array(112,315,66,40));
$piChart->setLabels(array("first", "second", "third","fourth"));
$piChart->setLegend(array("first", "second", "third","fourth"));
$piChart->setColors(array("ff3344", "11ff11", "22aacc", "3333aa"));
echo $piChart->getImgCode();
我的问题似乎是,当我使用上面的代码时,它在第$piChart = new gPieChart();
行跳到另一个wordpress插件自动加载器中,并且没有正确加载gPieChart.php
。
错误消息为:
[26-Jul-2016 20:39:59 UTC] PHP Fatal error: Class 'gPieChart' not found in /home/ubuntu/workspace/wp-content/plugins/wp-analytics-mail/sendTestMail.php on line 267
[26-Jul-2016 20:39:59 UTC] PHP Stack trace:
[26-Jul-2016 20:39:59 UTC] PHP 1. {main}() /home/ubuntu/workspace/index.php:0
[26-Jul-2016 20:39:59 UTC] PHP 2. require() /home/ubuntu/workspace/index.php:17
[26-Jul-2016 20:39:59 UTC] PHP 3. require_once() /home/ubuntu/workspace/wp-blog-header.php:19
[26-Jul-2016 20:39:59 UTC] PHP 4. include() /home/ubuntu/workspace/wp-includes/template-loader.php:75
[26-Jul-2016 20:39:59 UTC] PHP 5. include() /home/ubuntu/workspace/wp-content/themes/twentysixteen/page-analytics-analytics-mail.php:138
有什么建议可以让上面的代码工作吗?
我感谢你的回复!
以下是您的操作。
- 按照文档安装Composer,或者,如果你在Windows/Unix上,想办法在你的环境中安装它
- 在插件的根文件夹中创建名为
composer.json
的新文件(在本例中为wp-analytics-mail
) - 同样,根据文档,将其粘贴到
composer.json
中
{
"require": {
"gchartphp/gchartphp": "dev-master"
}
}
-
在命令行中,进入插件文件夹并运行
composer update
-
将
require 'vendor/autoload.php';
放在靠近主插件文件(sendTestMail.php
)顶部的位置 -
在同一个文件中向下,初始化并使用库:
$piChart = new gchart'gPieChart();