Question

I originally posted in the Sage forum here.

I'm trying to get a plugin to work but it's not, and I feel like it's a very general Namespace problem, and not something with the actual plugin, so I feel I could find success here.

The plugin (and it's code/repo) is here: https://github.com/ItinerisLtd/acf-gutenblocks

Any ideas how I can solve this error?

Fatal error: Uncaught Error: Class 'Itineris\AcfGutenblocks\Plugin' not found in /app/public/wp-content/plugins/acf-gutenblocks/acf-gutenblocks.php:32
Stack trace:
#0 /app/public/wp-includes/class-wp-hook.php(286): Itineris\AcfGutenblocks\{closure}('')
#1 /app/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
#2 /app/public/wp-includes/plugin.php(465): WP_Hook->do_action(Array)
#3 /app/public/wp-content/plugins/advanced-custom-fields-pro/acf.php(334): do_action('acf/init')
#4 /app/public/wp-includes/class-wp-hook.php(286): ACF->init('')
#5 /app/public/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#6 /app/public/wp-includes/plugin.php(465): WP_Hook->do_action(Array)
#7 /app/public/wp-settings.php(525): do_action('init')
#8 /app/public/wp-config.php(76): require_once('/app/public/wp-...')
#9 /app/public/wp-load.php(37): require_once('/app/public/wp-...')
#10 /app/public/wp-blog-header.php(13): require_once('/app/public/wp-...')
#11 /app/public/index.php(17): require('/app in /app/public/wp-content/plugins/acf-gutenblocks/acf-gutenblocks.php on line 32

I put the following code into my setup.php:

add_filter('acf_gutenblocks/blocks', function (array $blocks): array {
    $new_blocks = [
        Testimonial::class,
    ];
    return array_merge($blocks, $new_blocks);
});

If I remove the code above, my application works correctly.

My file structure, where app lives in wp-content/themes/[themename]/app:

app
-- Blocks
-- -- Testimonial
-- -- -- views
-- -- -- -- frontend.php
-- -- -- Testimonial.php
-- setup.php 

In my setup.php I have the following code:


Link to Gist with my current code here (setup.php, Testimonial.php, Frontend.php)


You'll notice also I put protected function registerFields(): array {} into the bottom of Testimonial.php, I hope that's a normal place to put it.

For the setup namespace, I've tried different variations: use Blocks\Testimonial\Testimonial; use app\blocks\testimonial\testimonial; (with lower case filenames also)

Edit The best idea I've got so far is that I've installed the plugin ( composer require itinerisltd/acf-gutenblocks ) to the directory sitename/app/public/wp-content/plugins/ and maybe the files in the Sage theme aren't picking up the namespace because it's in the main WordPress plugins.

Was it helpful?

Solution

You must specify in the roots sage theme composer.json the runtime where autoload can find the classes.

  "autoload": {
    "psr-4": {
      "App\\": "app/",
      "App\\Blocks\\": "app/Blocks/",
      "Itineris\\AcfGutenblocks\\": "../../plugins/acf-gutenblocks/src/",      
    }
  }

Don't forget to run composer dump-autoload

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top