0

I'm trying to add a shortcode to my Wordpress site to pull a piece of data from a Google spreadsheet and drop it into a page. To do this, I'm trying to use Sheetsu. The php libraries for Sheetsu are managed through Composer.

I've got a working piece of standalone code, but when I drop it in functions.php, like this...

function do_sheetsu() {
    require('vendor/autoload.php');
    use Sheetsu\Sheetsu;
    $sheetsu = new Sheetsu(['sheetId' => '8b297aa81110']);

    $response = $sheetsu->search(['id' => '2.05.1']);
    $collection = $response->getCollection();

    echo $collection->get(0)->answer;
    }
add_shortcode('sheetsu','do_sheetsu');

...it blanks my site. If I comment out the use Sheetsu\Sheetsu; line, my site comes back, but I get no output, and the error "PHP Fatal error: Class 'Sheetsu' not found" which I suppose makes perfectly good sense.

I know enough php to be able to break things, apparently, and my knowledge of Composer comes mostly from messing around with Flarum a little bit.

I'm sure I'm missing something obvious here, I'm guessing involving namespace declarations or something, but I can't put the pieces together.

I'm looking suspiciously at my composer.json file, as well—something doesn't seem right, but I'm not sure what to fix.

For the record, my composer.json, composer.lock, and vendor folder are in my theme folder with functions.php. My composer.json file looks like this:

{
    "require": {
        "emilianozublena/sheetsu-php": "^0.0.6"
    }
}

and I'm not sure it should.

But what's more troubling is finding a way around that use Sheetsu\Sheetsu line that seems to totally break Wordpress...

Thanks for any help!

1
  • did you run composer install from command line when on theme directory ? Commented Feb 10, 2019 at 14:20

1 Answer 1

1

I assume here you have installed the package using command line running composer install or composer require emilianozublena/sheetsu-php.

You cannot use the use php keyword inside function. The use keyword must be declared in the outermost scope of a file (the global scope). Refer to this answer for more detail here

So, in this condition you can chain the namespace while instantiating your class. In our case new Sheetsu(['sheetId' => '8b297aa81110']) becomes new \Sheetsu\Sheetsu(['sheetId' => '8b297aa81110']);

Try this code below

function do_sheetsu() {
    require('vendor/autoload.php');
    $sheetsu = new \Sheetsu\Sheetsu(['sheetId' => '8b297aa81110']);

    $response = $sheetsu->search(['id' => '2.05.1']);
    $collection = $response->getCollection();

    echo $collection->get(0)->answer;
}
add_shortcode('sheetsu','do_sheetsu');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.