1
votes

I have recently installed Timber on to my WordPress instance but whenever I try to run single.php for the timber-starter I get the following error:

Fatal error: Uncaught Error: Class 'Timber' not found in www\Website\wp\wp-content\plugins\timber-library\timber-starter-theme\single.php:12 Stack trace: #0 {main} thrown in www\Website\wp\wp-content\plugins\timber-library\timber-starter-theme\single.php on line 12

I have read that there can be issues with namespace and to update it to Timber\Timber. I have tried this also and get the same class not found for Timber\Timber. Interestingly, if I open it in PHPStorm I can navigate to the function directly from the class call so it is able to recognise it there.

Does anyone have any ideas? I've tried different versions of PHP, different versions of WordPress and installing via WP-Admin and manually. None of these options are fixing this issue. (Please note, I also get this error for going to index.php in this directory).

Here is the code from single.php with the added namespace definition.

<?php
/**
 * The Template for displaying all single posts
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since    Timber 0.1
 */ 

 use Timber\Timber; 

$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post;

if ( post_password_required( $post->ID ) ) {
    Timber::render( 'single-password.twig', $context );
} else {
    Timber::render( array( 'single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig' ), $context );
}

1
You should add your code of single.php to the question. - Second2None
Added in now. Other than the namespace line, everything else is the standard template that comes with timber. Also, thank you for fixing the formatting on my question! - CJGP
You should be able to update use Timber\Timber; to use Timber\Timber as Timber; - Second2None
Using use Timber\Timber as Timber; is not necessary, because Timber adds an alias Timber for Timber\Timber already (github.com/timber/timber/blob/…). - Gchtr
Did you install the theme into the plugin directory? Your path reads …/wp-content/plugins/timber-library/timber-starter-theme/. But it should probably be …/wp-content/themes/timber-starter/theme/, because you would install the starter theme as a theme. Also, when you say whenever I try to run single.php, do you access the single.php directly by typing in the URL to that file? - Gchtr

1 Answers

1
votes

It must seems trivial, but this error is commonly caused by a misinstallation. If you use Timber as a plugin, check if it is activated.

If you have installed trough a package manager, check you have used the right package name composer require timber/timber.

You can also try to remove your package and re-install it.

If all is correct, check your functions.php, myabe there is a misuse of the Timber Instance