0

I have a custom plugin that consists of various classes, with one class being called in a page template:

$chart = new MyCharts\Chart();
$csv = get_field( 'csv' ); // URI to .csv file
$chart->init( $csv );

MyChart class:

use League\Csv\Reader;

class Chart {

    private $id;
    protected $reader;
    protected $columns;
    protected $rows;
    protected $results;

    public function __construct() {
        add_action( 'wp_ajax_get_chart_data', [ $this, 'get_chart_data' ] );
        add_action( 'wp_ajax_nopriv_get_chart_data', [ $this, 'get_chart_data' ] );
        add_action( 'wp_footer', [ $this, 'load_chart_scripts' ] );
    }

    public function load_chart_scripts() {
        wp_enqueue_script( 'google_chart', 'https://www.gstatic.com/charts/loader.js' );
        wp_enqueue_script( 'bar_chart', plugins_url( '/js/bar-chart.js', __FILE__ ), 'jquery' );
    }

    public function init( $csv ) {
        $this->print_chart_container();
        $this->parse_csv( $csv );
    }

    private function print_chart_container() {
        echo '<div id="chart_div" style="width: 100%; height: 100%;"></div>';
    }

    private function parse_csv( $csv ) {
        $parse_csv_url = parse_url( $csv );

        try {
            $this->reader = Reader::createFromPath( ltrim( $parse_csv_url['path'], '/' ) );
            $this->rows = $this->reader->fetchAssoc();
            $this->columns = $this->reader->fetchOne( 0 ); // fetch first row as column names
        } catch ( \Exception $e ) {
            echo $e->getMessage();
        }
    }

    public function get_chart_data() {

        $result = new \stdClass();

        // dummy data for testing
        $result->columns = 'test';
        $result->rows = 'test';

        $this->results[] = $result;

        return wp_send_json( $this->results );
    }
}

Now, it seems the wp_ajax actions are being hit, but the callback function isn't, and I simply get 0 as an AJAX response.

Now remember this class is only every instantiated in the template.

I tried adding these AJAX actions into my main plugin class and the callback in an included class, but ideally I want to have it work in this standalone class.

Is this possibly due to adding the AJAX actions in the middle of my template?

Is there a way around this?

0

You can't hook into AJAX in a template, because the template's not going to be loaded by admin-ajax.php when the request is made. You need to hook into AJAX when the plugin is loaded.

Since your hooks are defined in the class constructor (whether nor not this is a good idea is another issue), you need to instantiate your class whenever the WordPress admin is loaded. You could no this by putting $chart = new MyCharts\Chart() directly in your main plugin file, or a file included by it.

  • Seeing as I'm going to have several pages, all using a different CSV file, would this be an issue? I assume once I add $chart = new MyCharts\Chart() into my plugin, can I call $chart->init() in my template? – Mr Pablo Sep 5 '17 at 11:21
  • Can you describe in your question what you're actually trying to accomplish? I can't tell from your code what the AJAX is even for, if you're outputting the CSV into the template. – Jacob Peattie Sep 5 '17 at 11:25
  • Ah, sorry. So I need to use google chart, but the data is dynamic depending on the CSV used (different columns and rows). So in my chart JS, I am using AJAX to pull the data in, because I cannot find a reliable way to include PHP into JS files (i'd need a twig equivalent for JS?) so AJAX was my first thought. Keeps the PHP out of the JS file. – Mr Pablo Sep 5 '17 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.