Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Including WordPress? #3944

Open
TrevorSqE opened this issue Mar 11, 2024 · 4 comments
Open

Including WordPress? #3944

TrevorSqE opened this issue Mar 11, 2024 · 4 comments

Comments

@TrevorSqE
Copy link

Sorry I don't know how to use Github message templates very well but let me try and explain. We have a template on a WordPress website that posts data to a PHP file, which generates a spreadsheet. It starts with this code:

	use PhpOffice\PhpSpreadsheet\Helper\Sample;
	use PhpOffice\PhpSpreadsheet\IOFactory;
	use PhpOffice\PhpSpreadsheet\Spreadsheet;
	use PhpOffice\PhpSpreadsheet\Style\Border;
	use PhpOffice\PhpSpreadsheet\Style\Fill;
	use PhpOffice\PhpSpreadsheet\Style\Font;
	use PhpOffice\PhpSpreadsheet\Style\Style;
	use PhpOffice\PhpSpreadsheet\Style\Alignment;
        $spreadsheet = new Spreadsheet();

However if we add this line at the top of the file, so we can integrate WordPress and use its functions:

require_once('../../../../wp-load.php');

We get this error:

PHP message: PHP Fatal error: Uncaught Error: Call to undefined method PhpOffice\PhpSpreadsheet\Spreadsheet::getDefinedName() in /www//public/wp-content/themes//vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Validations.php:109*

Is there a way we can use PHPSpreadsheet and WordPress in the same file?

Thank you!

@oleibman
Copy link
Collaborator

Not a WordPress user, but I will ask what could be a pertinent question. Before any use statements in my mainline program, I have the statement:

require __DIR__  . '/vendor/autoload.php'; // note your location may vary

You mention a require_once for WordPress; are you also including the statement above or equivalent?

@TrevorSqE
Copy link
Author

Oh thank you @oleibman, but I yes, I should have said. If we have this, then we get the error I reported earlier:

require_once('../../../../wp-load.php');  
require_once __DIR__ . '/../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
$spreadsheet = new Spreadsheet();

But then if we swap the first two lines round like this, and click the button to submit the data, no error happens - but nothing happens.

require_once __DIR__ . '/../vendor/autoload.php';
require_once('../../../../wp-load.php');  

use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\Style;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
$spreadsheet = new Spreadsheet();

@oleibman
Copy link
Collaborator

Sorry it wasn't a simple solution. I don't have any other ideas. Have you asked this question of WordPress?

@TrevorSqE
Copy link
Author

Thanks very much anyway! Not that easy to contact WordPress alas, but I'll try some Facebook groups too. Much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants