mindkomm / timmy

Advanced image handling for Timber.
MIT License
171 stars 13 forks source link

Fatal error: Uncaught Error: Call to undefined function Timmy\add_action() #10

Closed njbarrett closed 7 years ago

njbarrett commented 7 years ago

Hey guys,

Love your work. I'm having trouble getting Timmy working on my installation. I have installed Timmy and Timber both via Composer, the latest versions of both.

In function.php I have the autoloader set, however it seems Timmy is loaded before the wp functions are available?

Here's the full error:


( ! ) Fatal error: Uncaught Error: Call to undefined function Timmy\add_action() in /Users/nick/Sites/xx/vendor/mindkomm/timmy/lib/Timmy.php on line 19
( ! ) Error: Call to undefined function Timmy\add_action() in /Users/nick/Sites/xx/vendor/mindkomm/timmy/lib/Timmy.php on line 19
Call Stack
#   Time    Memory  Function    Location
1   0.0001  370528  {main}( )   .../index.php:0
2   0.0001  370816  require( '/Users/nick/Sites/xx/www/wp/wp-blog-header.php' ) .../index.php:5
3   0.0001  371168  require_once( '/Users/nick/Sites/xx/www/wp/wp-load.php' )   .../wp-blog-header.php:13
4   0.0002  371944  require_once( '/Users/nick/Sites/xx/www/wp-config.php' )    .../wp-load.php:42
5   0.0002  373712  require_once( '/Users/nick/Sites/xx/vendor/autoload.php' )  .../wp-config.php:7
6   0.0004  389808  ComposerAutoloaderInit89b2a1fa8ec074114b3ea6f13e74d114::getLoader( )    .../autoload.php:7
7   0.0015  579048  composerRequire89b2a1fa8ec074114b3ea6f13e74d114( )  .../autoload_real.php:56
8   0.0015  579720  require( '/Users/nick/Sites/xx/vendor/mindkomm/timmy/init.php' )    .../autoload_real.php:66
9   0.0021  667432  Timmy\Timmy->__construct( ) .../init.php:7
10  0.0028  780264  Timmy\Timmy->init( )    .../Timmy.php:13
gchtr commented 7 years ago

Hey @njbarrett

It seems like PHP is trying to call the function add_action() in Timmy’s namespace (Timmy\add_action()) instead of the global namespace, where all the functions of WordPress are. Timmy waits for WordPress to be loaded and hooks into the after_setup_theme action. The easiest solution would of course be to prefix all global functions that Timmy calls with a \, but I’d like to understand what’s going on. I suspect the problem lies somewhere else.

DevonVille commented 7 years ago

I'm having the same issue

Using PHP version 7.1 TImmy is installed in vendor folder in root directory by using bedrock as a custom folder structure with sage9 as a starter theme

gchtr commented 7 years ago

Okay. I will install Bedrock and look at this more closely.

gchtr commented 7 years ago

Alright. The problem was that Composer loaded init.php, which automatically initialized Timmy. Usually, when Timmy was installed in a theme, a developer would manually require vendor/autoload.php in functions.php and Timmy would initialize after WordPress was loaded. With Bedrock, Composer initializes earlier, when WordPress is not loaded yet. The workaround for this is to always initialize Timmy manually.

With the new version 0.12.1 you need to manually initialize Timmy in functions.php of your theme with:

new Timmy\Timmy();

You can add this right after you called new Timber\Timber();.

Thanks for raising this issue. I think this way Timmy will work in most "special" environments. Initializing Timmy automatically maybe was a good idea when I introduced it. I learnt a lot in the meantime and I think this change is the better way to do it 😊.

I’m closing this for now. In case it shouldn’t work, add your comments here.