• Hello, I’ve been recently getting this error on any edit.php page in which Nested Pages is used.

    I’m running WP 6.1.1 with PHP 8.1.13, and Nested Pages 3.2.3, though I’ve been using this PHP version for months and only bumped into the error lately, so I think either a new WP or Nested Pages update introduced this?

    Either way, it seems the lines mentioned in the error contain some code that is not compatible with PHP 8.

    Error:

    Fatal error: Uncaught TypeError: unserialize(): Argument #1 ($data) must be of type string, array given in /var/www/html/wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php:39
    Stack trace: 0 /var/www/html/wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php(39): unserialize(Array) 1 /var/www/html/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(317): NestedPages\Entities\Listing\ListingRepository->visiblePages('rankings') 2 /var/www/html/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(392): NestedPages\Entities\Listing\Listing->listOpening(Array, 0) 3 /var/www/html/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(369): NestedPages\Entities\Listing\Listing->listPostLevel() 4 /var/www/html/wp-content/plugins/wp-nested-pages/app/Views/listing.php(98): NestedPages\Entities\Listing\Listing->getPosts() 5 /var/www/html/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(280): include('/var/www/html/w…') 6 /var/www/html/wp-includes/class-wp-hook.php(308): NestedPages\Entities\Listing\Listing->listPosts('') 7 /var/www/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array) 8 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action(Array) 9 /var/www/html/wp-admin/admin.php(259): do_action('rankings_page_n…') 10 /var/www/html/wp-admin/edit.php(10): require_once('/var/www/html/w…') 11 {main} thrown in /var/www/html/wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php on line 39

    Thanks for your time!

Viewing 6 replies - 1 through 6 (of 6 total)
  • I discovered this error too and found it was related to my OPcache plugin Docket Cache. If I disable OPcache then Nested Pages starts working again. I didn’t have this problem in PHP 7.4 but it appeared after updating to 8.1. It’s unfortunate because I want to use OPcache for the overall performance of my site, but Nested Pages makes managing the site so much easier.

    WordPress version 6.2
    Active theme: Theme Name (version 17.0)
    Current plugin: Nested Pages (version 3.2.3)
    PHP version 8.1.17
    
    
    
    Error Details
    =============
    An error of type E_ERROR was caused in line 39 of the file /path/to/site/wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php. Error message: Uncaught TypeError: unserialize(): Argument #1 ($data) must be of type string, array given in /path/to/site/wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php:39
    Stack trace:
    #0 /path/to/site/wp-content/plugins/wp-nested-pages/app/Entities/Listing/ListingRepository.php(39): unserialize(Array)
    #1 /path/to/site/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(317): NestedPages\Entities\Listing\ListingRepository->visiblePages('page')
    #2 /path/to/site/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(392): NestedPages\Entities\Listing\Listing->listOpening(Array, 0)
    #3 /path/to/site/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(369): NestedPages\Entities\Listing\Listing->listPostLevel()
    #4 /path/to/site/wp-content/plugins/wp-nested-pages/app/Views/listing.php(98): NestedPages\Entities\Listing\Listing->getPosts()
    #5 /path/to/site/wp-content/plugins/wp-nested-pages/app/Entities/Listing/Listing.php(280): include('/home/soulartst...')
    #6 /path/to/site/wp-includes/class-wp-hook.php(308): NestedPages\Entities\Listing\Listing->listPosts('')
    #7 /path/to/site/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
    #8 /path/to/site/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
    #9 /path/to/site/wp-admin/admin.php(259): do_action('toplevel_page_n...')
    #10 {main}
      thrown
    Thread Starter Pelly Benassi

    (@plasmax)

    @kevinthom damn you’re right!

    @plasmax I have a potential fix here… I fed the error message and the file /app/Entities/Listing/ListingRepository.php into ChatGPT about it and asked it to resolve the incompatibility with PHP8. In approximately 5 seconds it wrote back:

    The problem is happening in the visiblePages($post_type) function when trying to unserialize() an array. If the user meta ‘np_visible_posts’ is not ‘1’, the code attempts to unserialize the value without confirming it’s a string.

    We can resolve this issue by first checking if the data returned from get_user_meta() is a serialized string or an array before attempting to unserialize it. Here’s how you can modify the visiblePages($post_type) method:

    /**
    * User's Toggled Pages
    */
    public function visiblePages($post_type)
    {
        $meta = get_user_meta(get_current_user_id(), 'np_visible_posts', true);
        if ( $meta == '1' ) return [];
        if ( is_serialized($meta) ) {
            $visible = unserialize($meta);
        } elseif ( is_array($meta) ) {
            $visible = $meta;
        } else {
            $visible = [];
        }
        if ( !isset($visible[$post_type]) ) $visible[$post_type] = [];
        return $visible[$post_type];
    }
    

    This code first checks if the get_user_meta() result $meta is ‘1’. If so, it returns an empty array. If not, it then checks if $meta is a serialized string with the is_serialized() function. If it is, it unserializes the string into the $visible variable. If $meta is already an array, it assigns it to the $visible variable. If $meta is neither a serialized string nor an array, it assigns an empty array to $visible. It then checks if $visible[$post_type] is set, if not it assigns an empty array to it, and finally returns $visible[$post_type].

    This change should fix the TypeError that’s been raised in your application.

    All I had to do after that was to disable Nested Pages, comment out the original visiblePages($post_type) function in /app/Entities/Listing/ListingRepository.php and paste in the replacement, and then re-enable Nested Pages. It seems to be working again. I haven’t experienced the error again.

    It’s not perfect because it doesn’t seem to remember the toggle state of my nested pages list anymore when I leave the All Pages list and come back to it, but at least it doesn’t crash.

    Update: Deleting a page from the Nested Pages list of pages now causes a “Critical Error” screen, but it doesn’t seem to do any real damage. I can just reload the list and see that the page has been deleted.

    Thread Starter Pelly Benassi

    (@plasmax)

    @kevinthom thanks for that, though what you get now was always the case for me, it throws the error but the page/post is actually deleted.

    Anyway I’ve moved to another hosting and I’m not using OPCache anymore, so the problem is gone for me, though it’d be nice if the dev barged in ??

    Take care!

    @plasmax Good to hear you’re not affected by the problem anymore. I used ChatGPT to fix the other error as well, but in the end, the dev quietly released a new version that seems to have fixed it altogether, which is great. Now I don’t need to use my little hack anymore!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Uncaught TypeError: unserialize(): Argument #1 ($data) must be of type string’ is closed to new replies.