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( 'b@b.com', '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';
}
  • 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? – Jacob Peattie Aug 25 at 14:05
  • I updated the question with the info. – Zeth Aug 25 at 14:07
  • Where/how is doSomethingBadass() used? – Jacob Peattie Aug 25 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 Aug 25 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 Aug 25 at 15:07
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.

| improve this answer | |
  • 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 Aug 28 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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