-1

I am trying to use the following github project in my wordpress plugin:

PHP Object wrapper for the Google Chart API

I tried the following:

 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();

My problem seems to be that when I am using the above code, it jumps at line $piChart = new gPieChart(); into another wordpress-plugins autoloader and does not load the gPieChart.php properly.

The error message is:

[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

Any suggestions how to make the above code work?

I appreciate your reply!

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • 1
    http://stackoverflow.com/questions/38598182/include-github-project-into-wordpress-plugin -- if you are not using composer you will need to build a autoloader yourself unless there is one there – David Jul 26 '16 at 22:04
  • @montrealist Please see my update. – Carol.Kar Jul 27 '16 at 04:37

1 Answers1

1

Here's what you do.

  1. Install Composer as per the doc or, if you're on Windows/Unix, figure out how to install the thing in your environment.
  2. Create new file called composer.json in the root folder of your plugin (in this case wp-analytics-mail)
  3. Again, as per the doc, paste this into the composer.json

{ "require": { "gchartphp/gchartphp": "dev-master" } }

  1. In the command line, get to the plugin folder and run composer update

  2. Put require 'vendor/autoload.php'; somewhere close to the top of your main plugin file (sendTestMail.php)

  3. Lower down in the same file, initialize and use the library:

$piChart = new gchart\gPieChart();

montrealist
  • 5,593
  • 12
  • 46
  • 68