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 );
}
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
Related
I'm new to OOP and MVC with PHP, and I'm currently learning by making my own custom MVC app for testing purposes. I’m not using Symfony yet, but I’ve integrated Twig and I have a problem with it when I call a 404 error outside a controller, in the two following cases :
When requested page doesn't exist on server (like http://localhost/test/)
When requested URL doesn't match with an existing controller and method (like http://localhost/test/task/)
I've got the following error :
Fatal error: Uncaught Error: Class 'Twig\Loader\FilesystemLoader' not found in /Applications/XAMPP/xamppfiles/htdocs/librairies/Renderer.php:32
Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/librairies/Renderer.php(21): Renderer::loadTwig()
#1 /Applications/XAMPP/xamppfiles/htdocs/librairies/Http.php(26): Renderer::render('errors/404', Array)
#2 /Applications/XAMPP/xamppfiles/htdocs/librairies/Application.php(35): Http::error404()
#3 /Applications/XAMPP/xamppfiles/htdocs/index.php(9): Application::process()
#4 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/librairies/Renderer.php on line 32
However, if I call the right controller with an existing method, everything works fine (like http://localhost/article/show/123).
If I call 404 error inside a controller method (e.g. if the $_GET ID of an article does not exist in the DB), everything works fine too and my 404 error template is correctly rendered.
How I call a 404 error ?
I use a static method Http::error404() that render my 404 error Twig template.
class Http
{
/**
* Display a 404 error - page not found
*
* #return void
*/
public static function error404(): void
{
header('HTTP/1.1 404 Not Found');
$pageTitle = "Erreur 404 - Page non-trouvée";
Renderer::render('errors/404', compact('pageTitle'));
exit;
}
}
Application class
My App use a mini-router named Application. It checks if the controller and the called method exists. If not, call 404 error with Http::error404(), and it is in this case that the above fatal error appears.
class Application
{
/**
* This is the app process, called in index.php file.
* Use controllers and methods directly in URL.
* URL are rewritten by .htaccess file at app root.
*
* Usage : https://example.com/controller/task/{optional_parameter}
* Ex : https://example.com/article/show/145
*
* #return void
*/
public static function process()
{
// By default, call homepage
$controllerName = 'ArticleController';
$task = 'home';
if (!empty($_GET['controller'])) {
$controllerName = ucfirst($_GET['controller'] . 'controller');
}
if (!empty($_GET['task'])) {
$task = $_GET['task'];
}
$controllerPath = "\controllers\\" . $controllerName;
// Check if this controller & method exists
if (!method_exists($controllerPath, $task)) {
Http::error404(); // <-- Here, i'm not in a controller (line 35)
}
$controller = new $controllerPath();
$controller->$task();
}
}
There is a strange thing here : if I define any controller before calling 404 error, everything works as I would like and my 404 error template is correctly rendered. But it’s not a clean and elegant solution...
// Check if this controller & method exists
if (!method_exists($controllerPath, $task)) {
new \Controllers\ArticleController(); // <-- Not clean, but solve my fatal error
Http::error404();
}
The same thing happens in my 404.php file, called by the server and declared in the .htaccess when a file is called and it doesn’t exist. My 404 PHP file just contains few lines :
require_once 'librairies/autoload.php';
Http::error404();
If I define any controller, it's working perfectly and fatal error disappears.
require_once 'librairies/autoload.php';
new Controllers\ArticleController();
Http::error404();
Render Class
This is my rendering class. The fatal error appears in this file, as shown in line 32 below, when Http class with error404 method render errors/404 Twig template.
require_once 'librairies/autoload.php';
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
class Renderer
{
/**
* Print a HTML template with $var injection
*
* #param string $path
* #param array $var
*/
public static function render(string $path, $var = [])
{
$template = self::loadTwig()->load("$path.html.twig");
echo $template->display($var);
}
/**
* Load Twig
*
* #return Environment
*/
public static function loadTwig()
{
$loader = new FilesystemLoader('templates'); // <-- Line 32 is here !
$twig = new Environment($loader, [
'auto_reload' => true,
'autoescape' => 'html'
]);
$twig->addFunction(
new TwigFunction('notificationDisplay', function() { return Notification::display(); })
);
return $twig;
}
}
I have only added the code that I think is related to my issue. Do not hesitate to ask if there are any points that need to be clarified.
I’ve been looking for a solution to this error for seven days and haven’t found a solution. Your help is my last resort. Thank you very much!
I removed all the many unnecessary require_once autoloader in app files, and kept only the one in index.php, as indicated by #NicoHaase.
From that point, Twig is no longer found and the same error message appears on all requests !
The solution was easy to find : in my index.php file, I was using the wrong autoloader. I was calling my custom autoloader instead of the one from Composer...
I just change :
require_once 'librairies/autoload.php';
\Application::process();
To :
require_once 'vendor/autoload.php';
\Application::process();
And everything works fine now ! Thanks to #NicoHaase and #DarkBee for putting me on the right track !
The Error my website displays is below:
Fatal error: Access level to Molla_Element_Section::get_html_tag()
must be protected (as in class Elementor\Element_Section) or weaker in
web.com/public_html/wp-content/plugins/molla-core/elementor/elements/section.php
on line 3668
As per the directory above the code on line 3668 is below:
/**
* Get HTML tag.
*
* Retrieve the section element HTML tag.
*
* #since 1.0
*/
private function get_html_tag() {
$html_tag = $this->get_settings( 'html_tag' );
if ( empty( $html_tag ) ) {
$html_tag = 'section';
}
return $html_tag;
}
Please help me fix this issue , I tried playing with elementor version (downgraded to check if this was the issue) but didn't help.
Take a look at Object Inheritance in php documentation:
The visibility of methods, properties and constants can be relaxed, e.g. a protected method can be marked as public, but they cannot be restricted, e.g. marking a public property as private.
Most likely Molla_Element_Section class inherits from Elementor\Element_Section and overwrites method get_html_tag. But it uses wrong access level.
get_html_tag method cannot be 'private' it has to be 'protected' or 'public'. As documentation says visibility cannot be restricted.
So I went ahead and rolled back the Elementor version to 3.3.1. Currently, everything works stable and the temporary solution seems working fine. If you are facing the same problem, here is what you need to do to downgrade Elementor.
Login to WP-Dashboard > Elementor > Tools > Version Control and Rollback version to 3.3.1
Click “Save” and make sure to clean the browser cache.
You should get a working website now!
I'm getting this weird message while trying to enter some of the pages of my WordPress.
Warning: require_once(/home3/alexismoyano/calendario/calendario/wp-content/themes/twentynineteen/includes/bootstrap.php): failed to open stream: No such file or directory in /home3/alexismoyano/calendario/calendario/wp-content/themes/Avada/functions.php on line 51
Fatal error: require_once(): Failed opening required '/home3/alexismoyano/calendario/calendario/wp-content/themes/twentynineteen/includes/bootstrap.php' (include_path='.:/opt/php70/lib/php') in /home3/alexismoyano/calendario/calendario/wp-content/themes/Avada/functions.php on line 51
I don't understand what could it be, it seems to be related to the theme "Twentnineteen" despite I'm not even using it.
This is what it's on LINE 51:
This is what it says on line 51:
LINE 51 : " require_once get_template_directory() . '/includes/bootstrap.php'; "
/**
* Check that the site meets the minimum requirements for the theme before proceeding.
* #since 6.0
*/
if ( version_compare( $GLOBALS['wp_version'], AVADA_MIN_WP_VER_REQUIRED, '<' ) || version_compare( PHP_VERSION, AVADA_MIN_PHP_VER_REQUIRED, '<' ) ) {
require_once get_template_directory() . '/includes/bootstrap-compat.php';
return;
}
/**
* Bootstrap the theme.
*
* #since 6.0
*/
require_once get_template_directory() . '/includes/bootstrap.php';
Does someone know how can I solve it?
Based on your comments, it looks like the twentynineteen theme is set as a template theme, and Avada is set as a child theme.
The function get_template_directory() is identical to get_stylesheet_directory() except for the fact it looks at the Template/Parent theme first instead of the current (child) theme.
Take a look at the documentation for Child Themes. You'll notice there's a comment block at the top of the style.css file that determines the parent/template theme, as an example:
/*
Theme Name: Some Child Theme
Author: John Doe
Author URI: http://example.com
Template: twentynineteen
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
You see in line in there Template: twentynineteen, that's telling your active theme to use that as a parent theme.
Now, I'm not familiar with Avada, but is it supposed to be a child theme of Twentynineteen? I don't believe so, but if it is, try reinstalling the twentynineteen theme. If it's not supposed to be, then make sure you don't have a Template: twentynineteen line in your style.css file.
If neither of those things work, you could also try replacing the get_template_{…}() functions with get_stylesheet_{…}() functions, but those would get reverted if/when you updated the theme.
I have a website, and when I go to the WordPress admin page and click on woocommerce-settings it shows this error:
Fatal error: Cannot declare class WC_Settings_General, because the
name is already in use in
/(hosting)/website/wp-content/plugins/woocommerce/includes/admin/settings/class-wc-settings-general.php
on line 0 The site is experiencing technical difficulties. Please
check your site admin email inbox for instructions.
The beginning of class-wc-settings-general.php looks like this:
<?php
/**
* WooCommerce General Settings
*
* #package WooCommerce/Admin
*/
defined( 'ABSPATH' ) || exit;
if ( class_exists( 'WC_Settings_General', false ) ) {
return new WC_Settings_General();
}
/**
* WC_Admin_Settings_General.
*/
class WC_Settings_General extends WC_Settings_Page {
/**
* Constructor.
*/
public function __construct() {
$this->id = 'general';
$this->label = __( 'General', 'woocommerce' );
parent::__construct();
}
/**
* Get settings array.
*
* #return array
*/
public function get_settings() {
etc. The webpage url that is generating this error is: https://www.(mywebsite).com/wp-admin/admin.php?page=wc-settings
I need to know how to resolve this issue and get to the woocommerce settings. I have other websites that have woocommerce and do not have this issue, and I do not know where the other place it is declared would be.
If you need to know the list of plugins, please, let me know.
Please do not flag as a duplicate post as this is a very specific issue regarding woocommerce and WordPress that the other posts I have looked at (around 8 others) do not fix. I have checked for require to change to require_once
Thank you in advance!
I had exactly the same error, except it was pointing to line 0. I switched theme to Storefront and deactivated all plugins except WooCommerce, which fixed the problem... then switched the theme back and gradually added plugins back in to find out which was the problem.
Hopefully this can work for you as well - it was just a conflict with me on an old plugin I thankfully wasn't using any more anyway.
I'm trying to create a script that would download and install wordpress + plugins + themes automatically.
All the stages until installing are working out. The files are downloaded and the wp-config.php is working - but when I try to run the wp install I get the following error:
Fatal error: Call to undefined method stdClass::add_query_var() in C:\Apache24\htdocs\wp\wp\wp-includes\taxonomy.php on line 371
From doing some research - it seems that the problem is finding the right way to include wordpress functions in my script - this is my code
define( 'WP_INSTALLING', true );
global $wp;
require_once( $directory . 'wp-blog-header.php' );
/** Load WordPress Bootstrap */
require_once( $directory . 'wp-load.php' );
/** Load WordPress Administration Upgrade API */
require_once( $directory . 'wp-admin/includes/upgrade.php' );
/** Load wpdb */
require_once( $directory . 'wp-includes/wp-db.php' );
var_dump($wp);
// WordPress installation
wp_install( $data[ 'weblog_title' ], $data['user_login'], $data['admin_email'], (int) $data[ 'blog_public' ], '', $data['admin_password'] );
in taxonomy line 371 theres a function call :
$wp->add_query_var( $args['query_var'] );
I suspect that $wp never gets defined.
Does anyone know what I should change to do that?
I made sure that it crashes on the wp_install - never goes past that in my script.