I am developing a Wordpress plugin, I have composer inside my plugin directory at root level. I have installed all of the packages that I'd like to use and then I have PHP use commands loading in each namespace at the top of my plugin file after auto loading the packages.
PHP error
[28-Jun-2017 10:09:37 UTC] PHP Fatal error: Class 'DrewM\MailChimp\MailChimp' not found in /home/xxx/public_html/wp-content/plugins/plugin-name/plugin-name.php on line 44
Plugin file structure
/plugin-name
'- vendor/
'- .gitignore
'- composer.json
'- plugin-name.php
composer.json contents
{
"require": {
"guzzlehttp/guzzle": "^6.3",
"theiconic/php-ga-measurement-protocol": "^2.0",
"mailgun/mailgun-php": "~2.3.4",
"stripe/stripe-php": "^4.13.0",
"mailchimp/mailchimp": "^2.0",
"drewm/mailchimp-api": "^2.4"
}
}
plugin-name.php contents
<?php
/*
Plugin Name: Plugin name
[...]
*/
require 'vendor/autoload.php';
use TheIconic\Tracking\GoogleAnalytics\Analytics;
use Mailgun\Mailgun;
use Stripe\Stripe;
use \DrewM\MailChimp\MailChimp;
Solved: In plugin-name.php, I needed to change
require 'vendor/autoload.php';
to
require plugin_dir_path(__FILE__).'vendor/autoload.php';
Because the current working directory is always the root of the Wordpress installation, not inside my plugin directory.
The reason I wasn't getting an error on the require line is because I had another similar vendor directory in the root of the installation.
Related
I'm developing a brand new Wordpress plugin and I would like to use Composer to autoload classes.
Here is the plugin directory heriarchy:
my composer.json content:
{
"autoload": {
"psr-4": {
"G4S_ECommerce\\": "src"
}
}
}
In the directory where composer.json is, on cmd, I execute:
composer install -> this generates the vendor/composer folder and the vendore/autoload.php.
composer composer dumpautoload -o -> outputs "Generated optimized autoload files containing 0 classes"
In the main file G4S_Ecommerce.php I put the following line:
require __DIR__.'/vendor/autoload.php';
In the same file I put
use G4S_Ecommerce\Includes\Ecommerce;
$starter = new Ecommerce();
but it leads me to a Fatal error: Uncaught Error: Class 'G4S_Ecommerce\Includes\Ecommerce' not found
Why the composer dumpautoload -o returns 0 classes? What am I doing wrong?
Thanks
First (it is not obvious from your files structure) you need to set a namespace for your Ecommerce class (i.e., G4S_Ecommerce/Includes)
Second, based on what you've declared in the autoload directive, composer is expecting to find the G4S_Ecommerce folder under the src folder, and in that folder you need to place your php class file with a name identical to the class name (i.e., Ecommerce).
So I've installed createsend-php for my theme via composer (I'm trying to learn how too use it) but can't get to the next stage.
I can see the API here -
/wp-content/themes/wonkhe2-theme/vendor/campaign-monitor/createsend-php/
composer file seems right to me -
"require": {
"php": ">=5.4.0",
"composer/installers": "~1.0",
"campaignmonitor/createsend-php": ">=6.0"
}
in /wp-content/themes/wonkhe2-theme/templates/content-signup-cm.php I've added
require_once 'csrest_campaigns.php'
And that returns
Fatal error: require_once(): Failed opening required 'csrest_campaigns.php' (include_path='.:/Applications/MAMP/bin/php/php7.2.7/lib/php') in /wp-content/themes/wonkhe2-theme/templates/content-signup-cm.php on line 5
Should the require_once path be different? I thought autoloader would set the paths and namespaces.
Using composer is new to me so apologies if I'm misunderstanding but any help appreciated.
you should not require individual classes insalled by composer. instead, right at the start of your code:
require_once 'vendor/autoload.php';
then you can just start using objects;
use Some\Class\Or\Other;
$object = new Other();
I'm trying to test out the monolog package using composer (full disclosure: this is my first try using composer) and am getting a 500 error when running this code:
<?php
// composer autoloader
require_once 'vendor/autoload.php';
// Shortcuts for simpler usage
use \Monolog\Logger;
use \Monolog\Formatter\LineFormatter;
use \Monolog\Handler\StreamHandler;
// Common logger
$log = new Logger('files');
// Line formatter without empty brackets in the end
$formatter = new LineFormatter(null, null, false, true);
// Debug level handler
$debugHandler = new StreamHandler('debug.log', Logger::DEBUG);
$debugHandler->setFormatter($formatter);
// Error level handler
$errorHandler = new StreamHandler('error.log', Logger::ERROR);
$errorHandler->setFormatter($formatter);
// This will have both DEBUG and ERROR messages
$log->pushHandler($debugHandler);
// This will have only ERROR messages
$log->pushHandler($errorHandler);
// The actual logging
$log->debug('I am debug');
$log->error('I am error', array('productId' => 123));
?>
I see in Dreamweaver that the three 'use' lines in monolog_test.php are highlighted in red:
The error message is:
In my /Applications/MAMP/logs/php_error.log:
[03-Mar-2018 14:10:05 America/Toronto] PHP Fatal error: Class
'Monolog\Logger' not found in
/Users/Ross/Dropbox/htdocs/wonderfest/secure/contest/monolog_test.php
on line 13
My file system setup looks like this:
I installed Composer globally on my Mac and I know the installation is good because I was able to add some packages using composer require . My composer.json file:
{
"require": {
"monolog/monolog": "1.0",
"phpfastcache/phpfastcache": "^6.1",
"mpdf/mpdf": "^7.0"
}
}
And my vendor\autoload.php:
<?php
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit348f040c8a2a7d48c0a311fb1af10c08::getLoader();
I guess my question is this: do I have to do anything other than install packages to make their namespaces available?
I updated monolog to the latest stable version and although the 3 'use' lines in Dreamweaver still show up as errors, it works. What threw me was this line on the monolog package page:
Monolog works with PHP 7.0 or above, use Monolog ^1.0 for PHP 5.3+ support.
I thought that meant that I needed to use v1.0 for my PHP 5.6.32, but when I removed that version constraint from the require command everything seems to work - I get the two logs in the same folder as the monolog_test.php file.
My new composer.json (with one additional packages installed):
{
"require": {
"monolog/monolog": "^1.23",
"mpdf/mpdf": "^7.0",
"phpfastcache/phpfastcache": "^6.1",
"phpmailer/phpmailer": "^6.0"
}
}
I'm trying to import a composer project into a plugin for wordpress. Is there a better way to do this than requiring every single asset? I do not plan to use composer to manage anything moving forward with this project, but I did use composer to install all requirements into the project folder (the plugin folder). Essentially, I'm trying to figure out how to convert a composer project to a wordpress plugin.
What you could do, load the autoloader from vendor/autoloader.php inside your functions.php. Then you could do something like this:
$loader = require $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
$loader->addPsr4('NAMESPACE\\', __DIR__ . '/app');
You are now autoloading the app folder.
How I use a composer packages into Wordpress plugin
I have a plugin's folder wp-content/plugins/stack-overflow/
Inside the folder I have the composer.json like next:
{
"name": "kumaxim/webhose-io-integration",
"license": "proprietary",
"authors": [
{
"name": "Your_name_here",
"email": "your_email_if#your_want.com"
}
],
"require": {
"php": "^5.4"
*** you can add other packages here ***
}
}
You have a plugin's file like next:
<?php
/**
* Plugin Name: The name of the plugin
*/
defined( 'WPINC' ) || die( 'Access restricted' );
function your_prefix_plugin_run() {
require_once __DIR__ . '/vendor/autoload.php';
}
your_prefix_plugin_run();
Pay attention, I strong recommend to call composer dependencies in plugin's bootstrap function. I have a problems in the past when I required it outside function.
What is next?
Add your dependencies and run composer install or composer update
I'm new to Composer and I'm really struggling to auto-load my classes with composer. What am I missing in the following process?
I installed the package in my PHP includes folder (which is outside the document root - I'm not sure if that matters) like this:
composer require monolog\monolog
It stated it completed successfully and I confirmed the project was added to my vendor folder.
My entire composer.json file looks like this:
{
"require": {
"monolog/monolog": "^1.22"
}
}
My entire test file looks like this:
<?php
require_once "vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
?>
And I get this error when I load the page:
Fatal error: Uncaught Error: Class 'Monolog\Logger' not found in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php:6 Stack trace: #0 {main} thrown in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php on line 6
It includes the vendor/autoload.php file without any error.
I've tried to run these commands in composer without any change:
composer update
composer dump-autoload -0
I've also tried it with different packages and I get the same error, so I'm pretty sure it has nothing to do with the monolog package.
Is there a step here I'm missing? I don't need to manually define which classes to autoload in a json file if I require them in composer, do I?
Edit 1:
As requested, here's the paths to my different files.
Path to the test page:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php
Path to the composer.json file (outside the document root but in my includes path):
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\composer.json
My vendor folder is here:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\vendor\
And inside my vendor folder I have these folders and file:
bin/
composer/
monolog/
psr/
autoload.php
You need to include autoload in your qbtest.php as following:
require_once "../wwwincludes/vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");