0

I have my theme, with a folder called classes, with a single class:

wp-content
|- themes
   |- mycustomtheme
      |- classes
         |- FooBar.php

In FooBar, I would like to use this package here: Soundasleep / HTML2Text.

And use it like this:

In FooBar.php

  doSomethingBadass( $some_html ){
    wp_mail( '[email protected]', 'test', \Soundasleep\Html2Text::convert( $some_html ) );
  }

But whatever I do, I keep getting this error:

Fatal error: Uncaught Error: Class 'Soundasleep\Html2Text' not found in /app/public/wp-content/themes/mycustomtheme/classes/FooBar.php:60


My attempt

I assume that it's something to do with the namespacing.

I'm including FooBar.php by requiring the class in the functions.php.

How do I get past this? And if possible, then please link to a documentation or a place where I could have figured this out. I've been on this for 2 hours now.


Update

I'm including Composer with these lines inside functions.php:

if( file_exists( get_template_directory() . '/vendor/autoload.php' ) ){
    require get_template_directory() . '/vendor/autoload.php';
}
6
  • How are you including that package? You mentioned Composer, so does your theme have a composer.json file and a vendor directory? Have you included the vendor/autoload.php file? Commented Aug 25, 2020 at 14:05
  • I updated the question with the info.
    – Zeth
    Commented Aug 25, 2020 at 14:07
  • Where/how is doSomethingBadass() used? Commented Aug 25, 2020 at 14:09
  • In /wp-content/themes/mycustomtheme/inc/some-other-file.php ... And I've included that in functions.php. Should I set a namespace in there? Or write: use Soundasleep\Html2Text in there?
    – Zeth
    Commented Aug 25, 2020 at 14:13
  • Is your theme a child theme? Have you confirmed that the package supports auto-loading, and that the autoloader is indeed loaded? Are you loading the autoloader before everything else?
    – Tom J Nowell
    Commented Aug 25, 2020 at 15:07

1 Answer 1

1

Your code uses the conditional:

if( file_exists())

So are you sure you are using the correct path to autoload.php as it will stop if not found and you won't get the usual error thrown by require?

Personally I'd be expecting to see your vendor dir outside of the wordpress install.

1
  • 1
    Moving the Vendor-directory fixed it, so I reference the html2text like so: \Soundasleep\Html2Text::convert( $some_html ). So for anyone else... Move composer.json to the root, so the vendor-directory will be there as well.
    – Zeth
    Commented Aug 28, 2020 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.